blob: b321e9635f7622781fd4f278a81cd5d15a4cef97 [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;
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -080031extern const ipmi::sensor::EntityInfoMap entities;
Ratan Guptae0cc8552018-01-22 14:23:04 +053032
Tom Josephbe703f72017-03-09 12:34:35 +053033using namespace phosphor::logging;
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -050034using InternalFailure =
35 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austenac4604a2015-10-13 12:43:27 -050036
Patrick Venture0b02be92018-08-31 11:55:55 -070037void register_netfn_sen_functions() __attribute__((constructor));
Chris Austenac4604a2015-10-13 12:43:27 -050038
Patrick Venture0b02be92018-08-31 11:55:55 -070039struct sensorTypemap_t
40{
Chris Austen0012e9b2015-10-22 01:37:46 -050041 uint8_t number;
Chris Austend7cf0e42015-11-07 14:27:12 -060042 uint8_t typecode;
Chris Austen0012e9b2015-10-22 01:37:46 -050043 char dbusname[32];
Patrick Venture0b02be92018-08-31 11:55:55 -070044};
Chris Austen0012e9b2015-10-22 01:37:46 -050045
Chris Austen0012e9b2015-10-22 01:37:46 -050046sensorTypemap_t g_SensorTypeMap[] = {
47
Chris Austend7cf0e42015-11-07 14:27:12 -060048 {0x01, 0x6F, "Temp"},
49 {0x0C, 0x6F, "DIMM"},
50 {0x0C, 0x6F, "MEMORY_BUFFER"},
51 {0x07, 0x6F, "PROC"},
52 {0x07, 0x6F, "CORE"},
53 {0x07, 0x6F, "CPU"},
54 {0x0F, 0x6F, "BootProgress"},
Patrick Venture0b02be92018-08-31 11:55:55 -070055 {0xe9, 0x09, "OccStatus"}, // E9 is an internal mapping to handle sensor
56 // type code os 0x09
Chris Austend7cf0e42015-11-07 14:27:12 -060057 {0xC3, 0x6F, "BootCount"},
58 {0x1F, 0x6F, "OperatingSystemStatus"},
Chris Austen800ba712015-12-03 15:31:00 -060059 {0x12, 0x6F, "SYSTEM_EVENT"},
60 {0xC7, 0x03, "SYSTEM"},
61 {0xC7, 0x03, "MAIN_PLANAR"},
Chris Austen10ccc0f2015-12-10 18:27:04 -060062 {0xC2, 0x6F, "PowerCap"},
Tom Joseph558184e2017-09-01 13:45:05 +053063 {0x0b, 0xCA, "PowerSupplyRedundancy"},
Jayanth Othayoth0661beb2017-03-22 06:00:58 -050064 {0xDA, 0x03, "TurboAllowed"},
Tom Joseph558184e2017-09-01 13:45:05 +053065 {0xD8, 0xC8, "PowerSupplyDerating"},
Chris Austend7cf0e42015-11-07 14:27:12 -060066 {0xFF, 0x00, ""},
Chris Austen0012e9b2015-10-22 01:37:46 -050067};
68
Patrick Venture0b02be92018-08-31 11:55:55 -070069struct sensor_data_t
70{
Chris Austenac4604a2015-10-13 12:43:27 -050071 uint8_t sennum;
Patrick Venture0b02be92018-08-31 11:55:55 -070072} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050073
Patrick Venture0b02be92018-08-31 11:55:55 -070074struct sensorreadingresp_t
75{
Chris Austen10ccc0f2015-12-10 18:27:04 -060076 uint8_t value;
77 uint8_t operation;
78 uint8_t indication[2];
Patrick Venture0b02be92018-08-31 11:55:55 -070079} __attribute__((packed));
Chris Austenac4604a2015-10-13 12:43:27 -050080
Patrick Venture83a0b842019-07-19 18:37:15 -070081const ipmi::sensor::EntityInfoMap& getIpmiEntityRecords()
82{
83 return entities;
84}
85
Patrick Venture0b02be92018-08-31 11:55:55 -070086int get_bus_for_path(const char* path, char** busname)
87{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070088 return mapper_get_service(bus, path, busname);
89}
Tomd700e762016-09-20 18:24:13 +053090
Emily Shaffer2ae09b92017-04-05 15:09:41 -070091// Use a lookup table to find the interface name of a specific sensor
92// This will be used until an alternative is found. this is the first
93// step for mapping IPMI
Patrick Venture0b02be92018-08-31 11:55:55 -070094int find_openbmc_path(uint8_t num, dbus_interface_t* interface)
95{
Emily Shaffer2ae09b92017-04-05 15:09:41 -070096 int rc;
97
Emily Shaffer2ae09b92017-04-05 15:09:41 -070098 const auto& sensor_it = sensors.find(num);
99 if (sensor_it == sensors.end())
100 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500101 // The sensor map does not contain the sensor requested
102 return -EINVAL;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700103 }
104
105 const auto& info = sensor_it->second;
106
Patrick Williams8451edf2017-06-13 09:01:06 -0500107 char* busname = nullptr;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700108 rc = get_bus_for_path(info.sensorPath.c_str(), &busname);
Patrick Venture0b02be92018-08-31 11:55:55 -0700109 if (rc < 0)
110 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700111 std::fprintf(stderr, "Failed to get %s busname: %s\n",
112 info.sensorPath.c_str(), busname);
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700113 goto final;
114 }
115
116 interface->sensortype = info.sensorType;
117 strcpy(interface->bus, busname);
118 strcpy(interface->path, info.sensorPath.c_str());
119 // Take the interface name from the beginning of the DbusInterfaceMap. This
120 // works for the Value interface but may not suffice for more complex
121 // sensors.
122 // tracked https://github.com/openbmc/phosphor-host-ipmid/issues/103
Patrick Venture0b02be92018-08-31 11:55:55 -0700123 strcpy(interface->interface,
124 info.propertyInterfaces.begin()->first.c_str());
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700125 interface->sensornumber = num;
126
127final:
128 free(busname);
129 return rc;
130}
131
Tomd700e762016-09-20 18:24:13 +0530132/////////////////////////////////////////////////////////////////////
133//
134// Routines used by ipmi commands wanting to interact on the dbus
135//
136/////////////////////////////////////////////////////////////////////
Patrick Venture0b02be92018-08-31 11:55:55 -0700137int set_sensor_dbus_state_s(uint8_t number, const char* method,
138 const char* value)
139{
Tomd700e762016-09-20 18:24:13 +0530140
141 dbus_interface_t a;
142 int r;
143 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700144 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530145
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700146 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530147
Patrick Venture0b02be92018-08-31 11:55:55 -0700148 if (r < 0)
149 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700150 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530151 return 0;
152 }
153
Patrick Venture0b02be92018-08-31 11:55:55 -0700154 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
155 method);
156 if (r < 0)
157 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700158 std::fprintf(stderr, "Failed to create a method call: %s",
159 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530160 goto final;
161 }
162
163 r = sd_bus_message_append(m, "v", "s", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700164 if (r < 0)
165 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700166 std::fprintf(stderr, "Failed to create a input parameter: %s",
167 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530168 goto final;
169 }
170
Tomd700e762016-09-20 18:24:13 +0530171 r = sd_bus_call(bus, m, 0, &error, NULL);
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 call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530175 }
176
177final:
178 sd_bus_error_free(&error);
179 m = sd_bus_message_unref(m);
180
181 return 0;
182}
Patrick Venture0b02be92018-08-31 11:55:55 -0700183int set_sensor_dbus_state_y(uint8_t number, const char* method,
184 const uint8_t value)
185{
Tomd700e762016-09-20 18:24:13 +0530186
187 dbus_interface_t a;
188 int r;
189 sd_bus_error error = SD_BUS_ERROR_NULL;
Patrick Venture0b02be92018-08-31 11:55:55 -0700190 sd_bus_message* m = NULL;
Tomd700e762016-09-20 18:24:13 +0530191
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700192 r = find_openbmc_path(number, &a);
Tomd700e762016-09-20 18:24:13 +0530193
Patrick Venture0b02be92018-08-31 11:55:55 -0700194 if (r < 0)
195 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700196 std::fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
Tomd700e762016-09-20 18:24:13 +0530197 return 0;
198 }
199
Patrick Venture0b02be92018-08-31 11:55:55 -0700200 r = sd_bus_message_new_method_call(bus, &m, a.bus, a.path, a.interface,
201 method);
202 if (r < 0)
203 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700204 std::fprintf(stderr, "Failed to create a method call: %s",
205 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530206 goto final;
207 }
208
209 r = sd_bus_message_append(m, "v", "i", value);
Patrick Venture0b02be92018-08-31 11:55:55 -0700210 if (r < 0)
211 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700212 std::fprintf(stderr, "Failed to create a input parameter: %s",
213 strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530214 goto final;
215 }
216
Tomd700e762016-09-20 18:24:13 +0530217 r = sd_bus_call(bus, m, 0, &error, NULL);
Patrick Venture0b02be92018-08-31 11:55:55 -0700218 if (r < 0)
219 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700220 std::fprintf(stderr, "12 Failed to call the method: %s", strerror(-r));
Tomd700e762016-09-20 18:24:13 +0530221 }
222
223final:
224 sd_bus_error_free(&error);
225 m = sd_bus_message_unref(m);
226
227 return 0;
228}
229
Patrick Venture0b02be92018-08-31 11:55:55 -0700230uint8_t dbus_to_sensor_type(char* p)
231{
Chris Austenac4604a2015-10-13 12:43:27 -0500232
Patrick Venture0b02be92018-08-31 11:55:55 -0700233 sensorTypemap_t* s = g_SensorTypeMap;
234 char r = 0;
235 while (s->number != 0xFF)
236 {
237 if (!strcmp(s->dbusname, p))
238 {
Tom Joseph558184e2017-09-01 13:45:05 +0530239 r = s->typecode;
Patrick Venture0b02be92018-08-31 11:55:55 -0700240 break;
Chris Austenac4604a2015-10-13 12:43:27 -0500241 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500242 s++;
Chris Austenac4604a2015-10-13 12:43:27 -0500243 }
244
Chris Austen0012e9b2015-10-22 01:37:46 -0500245 if (s->number == 0xFF)
246 printf("Failed to find Sensor Type %s\n", p);
Chris Austenac4604a2015-10-13 12:43:27 -0500247
Chris Austen0012e9b2015-10-22 01:37:46 -0500248 return r;
Chris Austenac4604a2015-10-13 12:43:27 -0500249}
250
Patrick Venture0b02be92018-08-31 11:55:55 -0700251uint8_t get_type_from_interface(dbus_interface_t dbus_if)
252{
Chris Austen0012e9b2015-10-22 01:37:46 -0500253
Brad Bishop56003452016-10-05 21:49:19 -0400254 uint8_t type;
Chris Austen0012e9b2015-10-22 01:37:46 -0500255
Chris Austen0012e9b2015-10-22 01:37:46 -0500256 // This is where sensors that do not exist in dbus but do
257 // exist in the host code stop. This should indicate it
258 // is not a supported sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700259 if (dbus_if.interface[0] == 0)
260 {
261 return 0;
262 }
Chris Austen0012e9b2015-10-22 01:37:46 -0500263
Emily Shaffer71174412017-04-05 15:10:40 -0700264 // Fetch type from interface itself.
265 if (dbus_if.sensortype != 0)
266 {
267 type = dbus_if.sensortype;
Patrick Venture0b02be92018-08-31 11:55:55 -0700268 }
269 else
270 {
Chris Austen0012e9b2015-10-22 01:37:46 -0500271 // Non InventoryItems
Patrick Venture4491a462018-10-13 13:00:42 -0700272 char* p = strrchr(dbus_if.path, '/');
Patrick Venture0b02be92018-08-31 11:55:55 -0700273 type = dbus_to_sensor_type(p + 1);
Chris Austen0012e9b2015-10-22 01:37:46 -0500274 }
275
Brad Bishop56003452016-10-05 21:49:19 -0400276 return type;
Patrick Venture0b02be92018-08-31 11:55:55 -0700277}
Chris Austen0012e9b2015-10-22 01:37:46 -0500278
Emily Shaffer391f3302017-04-03 10:27:08 -0700279// Replaces find_sensor
Patrick Venture0b02be92018-08-31 11:55:55 -0700280uint8_t find_type_for_sensor_number(uint8_t num)
281{
Emily Shaffer391f3302017-04-03 10:27:08 -0700282 int r;
283 dbus_interface_t dbus_if;
Emily Shaffer2ae09b92017-04-05 15:09:41 -0700284 r = find_openbmc_path(num, &dbus_if);
Patrick Venture0b02be92018-08-31 11:55:55 -0700285 if (r < 0)
286 {
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700287 std::fprintf(stderr, "Could not find sensor %d\n", num);
Lei YU91875f72018-04-03 15:14:49 +0800288 return 0;
Emily Shaffer391f3302017-04-03 10:27:08 -0700289 }
290 return get_type_from_interface(dbus_if);
291}
292
Chris Austen0012e9b2015-10-22 01:37:46 -0500293ipmi_ret_t ipmi_sen_get_sensor_type(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700294 ipmi_request_t request,
295 ipmi_response_t response,
296 ipmi_data_len_t data_len,
297 ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500298{
Patrick Ventured99148b2018-10-13 10:06:13 -0700299 auto reqptr = static_cast<sensor_data_t*>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500300 ipmi_ret_t rc = IPMI_CC_OK;
301
Patrick Venture0b02be92018-08-31 11:55:55 -0700302 printf("IPMI GET_SENSOR_TYPE [0x%02X]\n", reqptr->sennum);
Chris Austenac4604a2015-10-13 12:43:27 -0500303
304 // TODO Not sure what the System-event-sensor is suppose to return
305 // need to ask Hostboot team
Patrick Venture0b02be92018-08-31 11:55:55 -0700306 unsigned char buf[] = {0x00, 0x6F};
Chris Austenac4604a2015-10-13 12:43:27 -0500307
Emily Shaffer391f3302017-04-03 10:27:08 -0700308 buf[0] = find_type_for_sensor_number(reqptr->sennum);
Chris Austen0012e9b2015-10-22 01:37:46 -0500309
310 // HACK UNTIL Dbus gets updated or we find a better way
Patrick Venture0b02be92018-08-31 11:55:55 -0700311 if (buf[0] == 0)
312 {
Chris Austen800ba712015-12-03 15:31:00 -0600313 rc = IPMI_CC_SENSOR_INVALID;
Chris Austen0012e9b2015-10-22 01:37:46 -0500314 }
315
Chris Austenac4604a2015-10-13 12:43:27 -0500316 *data_len = sizeof(buf);
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700317 std::memcpy(response, &buf, *data_len);
Chris Austenac4604a2015-10-13 12:43:27 -0500318
Chris Austenac4604a2015-10-13 12:43:27 -0500319 return rc;
320}
321
Patrick Venture0b02be92018-08-31 11:55:55 -0700322const std::set<std::string> analogSensorInterfaces = {
Emily Shaffercc941e12017-06-14 13:06:26 -0700323 "xyz.openbmc_project.Sensor.Value",
Patrick Venturee9a64052017-08-18 19:17:27 -0700324 "xyz.openbmc_project.Control.FanPwm",
Emily Shaffercc941e12017-06-14 13:06:26 -0700325};
326
327bool isAnalogSensor(const std::string& interface)
328{
329 return (analogSensorInterfaces.count(interface));
330}
331
Patrick Venture0b02be92018-08-31 11:55:55 -0700332ipmi_ret_t setSensorReading(void* request)
Tom Josephbe703f72017-03-09 12:34:35 +0530333{
Tom Joseph816e92b2017-09-06 19:23:00 +0530334 ipmi::sensor::SetSensorReadingReq cmdData =
Patrick Venture0b02be92018-08-31 11:55:55 -0700335 *(static_cast<ipmi::sensor::SetSensorReadingReq*>(request));
Tom Josephbe703f72017-03-09 12:34:35 +0530336
337 // Check if the Sensor Number is present
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500338 const auto iter = sensors.find(cmdData.number);
Tom Josephbe703f72017-03-09 12:34:35 +0530339 if (iter == sensors.end())
340 {
341 return IPMI_CC_SENSOR_INVALID;
342 }
343
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500344 try
345 {
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500346 if (ipmi::sensor::Mutability::Write !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700347 (iter->second.mutability & ipmi::sensor::Mutability::Write))
Jayanth Othayoth0922bde2018-04-02 07:59:34 -0500348 {
349 log<level::ERR>("Sensor Set operation is not allowed",
350 entry("SENSOR_NUM=%d", cmdData.number));
351 return IPMI_CC_ILLEGAL_COMMAND;
352 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500353 return iter->second.updateFunc(cmdData, iter->second);
354 }
355 catch (InternalFailure& e)
356 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700357 log<level::ERR>("Set sensor failed",
358 entry("SENSOR_NUM=%d", cmdData.number));
359 commit<InternalFailure>();
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500360 }
Tom Joseph82024322017-09-28 20:07:29 +0530361 catch (const std::runtime_error& e)
362 {
363 log<level::ERR>(e.what());
364 }
Dhruvaraj Subhashchandran18e99992017-08-09 09:10:47 -0500365
366 return IPMI_CC_UNSPECIFIED_ERROR;
Tom Josephbe703f72017-03-09 12:34:35 +0530367}
Chris Austenac4604a2015-10-13 12:43:27 -0500368
Chris Austen0012e9b2015-10-22 01:37:46 -0500369ipmi_ret_t ipmi_sen_set_sensor(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700370 ipmi_request_t request, ipmi_response_t response,
371 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austenac4604a2015-10-13 12:43:27 -0500372{
Patrick Ventured99148b2018-10-13 10:06:13 -0700373 auto reqptr = static_cast<sensor_data_t*>(request);
Chris Austenac4604a2015-10-13 12:43:27 -0500374
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530375 log<level::DEBUG>("IPMI SET_SENSOR",
376 entry("SENSOR_NUM=0x%02x", reqptr->sennum));
Chris Austenac4604a2015-10-13 12:43:27 -0500377
Tom Josephbe703f72017-03-09 12:34:35 +0530378 /*
379 * This would support the Set Sensor Reading command for the presence
380 * and functional state of Processor, Core & DIMM. For the remaining
381 * sensors the existing support is invoked.
382 */
383 auto ipmiRC = setSensorReading(request);
384
Patrick Venture0b02be92018-08-31 11:55:55 -0700385 if (ipmiRC == IPMI_CC_SENSOR_INVALID)
Tom Josephbe703f72017-03-09 12:34:35 +0530386 {
387 updateSensorRecordFromSSRAESC(reqptr);
388 ipmiRC = IPMI_CC_OK;
389 }
Chris Austen8a45e7c2015-10-15 00:31:46 -0500390
Patrick Venture0b02be92018-08-31 11:55:55 -0700391 *data_len = 0;
Tom Josephbe703f72017-03-09 12:34:35 +0530392 return ipmiRC;
Chris Austenac4604a2015-10-13 12:43:27 -0500393}
394
Tom Joseph3ee668f2018-03-02 19:49:17 +0530395ipmi_ret_t ipmi_sen_get_sensor_reading(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture0b02be92018-08-31 11:55:55 -0700396 ipmi_request_t request,
397 ipmi_response_t response,
398 ipmi_data_len_t data_len,
399 ipmi_context_t context)
Tom Joseph3ee668f2018-03-02 19:49:17 +0530400{
Patrick Ventured99148b2018-10-13 10:06:13 -0700401 auto reqptr = static_cast<sensor_data_t*>(request);
402 auto resp = static_cast<sensorreadingresp_t*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700403 ipmi::sensor::GetSensorResponse getResponse{};
Tom Joseph3ee668f2018-03-02 19:49:17 +0530404 static constexpr auto scanningEnabledBit = 6;
405
406 const auto iter = sensors.find(reqptr->sennum);
407 if (iter == sensors.end())
408 {
Adriana Kobylakba23ff72018-09-12 12:58:43 -0500409 return IPMI_CC_SENSOR_INVALID;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530410 }
411 if (ipmi::sensor::Mutability::Read !=
Patrick Venture0b02be92018-08-31 11:55:55 -0700412 (iter->second.mutability & ipmi::sensor::Mutability::Read))
Tom Joseph3ee668f2018-03-02 19:49:17 +0530413 {
Jayanth Othayoth6ccf8812018-04-05 22:58:48 -0500414 return IPMI_CC_ILLEGAL_COMMAND;
Tom Joseph3ee668f2018-03-02 19:49:17 +0530415 }
416
417 try
418 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700419 getResponse = iter->second.getFunc(iter->second);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530420 *data_len = getResponse.size();
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700421 std::memcpy(resp, getResponse.data(), *data_len);
Tom Joseph3ee668f2018-03-02 19:49:17 +0530422 resp->operation = 1 << scanningEnabledBit;
423 return IPMI_CC_OK;
424 }
Brandon Kim9cf85622019-06-19 12:05:08 -0700425#ifdef UPDATE_FUNCTIONAL_ON_FAIL
426 catch (const SensorFunctionalError& e)
427 {
428 return IPMI_CC_RESPONSE_ERROR;
429 }
430#endif
Tom Joseph3ee668f2018-03-02 19:49:17 +0530431 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
Vernon Maueryf442e112019-04-09 11:44:36 -0700457 double warnLow = std::visit(ipmi::VariantToDoubleVisitor(),
458 warnThresholds["WarningLow"]);
459 double warnHigh = std::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);
Vernon Maueryf442e112019-04-09 11:44:36 -0700482 double critLow = std::visit(ipmi::VariantToDoubleVisitor(),
483 critThresholds["CriticalLow"]);
484 double critHigh = std::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
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000568/** @brief implements the get SDR Info command
569 * @param count - Operation
570 *
571 * @returns IPMI completion code plus response data
572 * - sdrCount - sensor/SDR count
573 * - lunsAndDynamicPopulation - static/Dynamic sensor population flag
574 */
575ipmi::RspType<uint8_t, // respcount
576 uint8_t // dynamic population flags
577 >
578 ipmiSensorGetDeviceSdrInfo(std::optional<uint8_t> count)
Emily Shafferd06e0e72017-04-05 09:08:57 -0700579{
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000580 uint8_t sdrCount;
581 // multiple LUNs not supported.
582 constexpr uint8_t lunsAndDynamicPopulation = 1;
583 constexpr uint8_t getSdrCount = 0x01;
584 constexpr uint8_t getSensorCount = 0x00;
585
586 if (count.value_or(0) == getSdrCount)
Emily Shafferd06e0e72017-04-05 09:08:57 -0700587 {
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000588 // Get SDR count. This returns the total number of SDRs in the device.
Patrick Venture83a0b842019-07-19 18:37:15 -0700589 const auto& entityRecords = getIpmiEntityRecords();
590 sdrCount = sensors.size() + frus.size() + entityRecords.size();
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000591 }
592 else if (count.value_or(0) == getSensorCount)
593 {
594 // Get Sensor count. This returns the number of sensors
595 sdrCount = sensors.size();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700596 }
597 else
598 {
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000599 return ipmi::responseInvalidCommandOnLun();
Emily Shafferd06e0e72017-04-05 09:08:57 -0700600 }
601
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000602 return ipmi::responseSuccess(sdrCount, lunsAndDynamicPopulation);
Emily Shafferd06e0e72017-04-05 09:08:57 -0700603}
604
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000605/** @brief implements the reserve SDR command
606 * @returns IPMI completion code plus response data
607 * - reservationID - reservation ID
608 */
609ipmi::RspType<uint16_t> ipmiSensorReserveSdr()
Emily Shaffera344afc2017-04-13 15:09:39 -0700610{
611 // A constant reservation ID is okay until we implement add/remove SDR.
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000612 constexpr uint16_t reservationID = 1;
Emily Shaffera344afc2017-04-13 15:09:39 -0700613
jayaprakash Mutyalad9578232019-05-13 20:22:50 +0000614 return ipmi::responseSuccess(reservationID);
Emily Shaffera344afc2017-04-13 15:09:39 -0700615}
Chris Austenac4604a2015-10-13 12:43:27 -0500616
Patrick Venture0b02be92018-08-31 11:55:55 -0700617void setUnitFieldsForObject(const ipmi::sensor::Info* info,
618 get_sdr::SensorDataFullRecordBody* body)
Emily Shaffercc941e12017-06-14 13:06:26 -0700619{
Tom Josephdc212b22018-02-16 09:59:57 +0530620 namespace server = sdbusplus::xyz::openbmc_project::Sensor::server;
621 try
Emily Shaffercc941e12017-06-14 13:06:26 -0700622 {
Tom Josephdc212b22018-02-16 09:59:57 +0530623 auto unit = server::Value::convertUnitFromString(info->unit);
624 // Unit strings defined in
625 // phosphor-dbus-interfaces/xyz/openbmc_project/Sensor/Value.interface.yaml
626 switch (unit)
Emily Shaffercc941e12017-06-14 13:06:26 -0700627 {
Tom Josephdc212b22018-02-16 09:59:57 +0530628 case server::Value::Unit::DegreesC:
629 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_DEGREES_C;
630 break;
631 case server::Value::Unit::RPMS:
Kirill Pakhomov812e44c2018-10-22 16:25:35 +0300632 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_RPM;
Tom Josephdc212b22018-02-16 09:59:57 +0530633 break;
634 case server::Value::Unit::Volts:
635 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_VOLTS;
636 break;
637 case server::Value::Unit::Meters:
638 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_METERS;
639 break;
640 case server::Value::Unit::Amperes:
641 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_AMPERES;
642 break;
643 case server::Value::Unit::Joules:
644 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_JOULES;
645 break;
646 case server::Value::Unit::Watts:
647 body->sensor_units_2_base = get_sdr::SENSOR_UNIT_WATTS;
648 break;
649 default:
650 // Cannot be hit.
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700651 std::fprintf(stderr, "Unknown value unit type: = %s\n",
652 info->unit.c_str());
Emily Shaffercc941e12017-06-14 13:06:26 -0700653 }
654 }
Patrick Venture64678b82018-10-13 13:11:32 -0700655 catch (const sdbusplus::exception::InvalidEnumString& e)
Emily Shaffercc941e12017-06-14 13:06:26 -0700656 {
Tom Josephdc212b22018-02-16 09:59:57 +0530657 log<level::WARNING>("Warning: no unit provided for sensor!");
Emily Shaffercc941e12017-06-14 13:06:26 -0700658 }
Emily Shaffercc941e12017-06-14 13:06:26 -0700659}
660
Patrick Venture0b02be92018-08-31 11:55:55 -0700661ipmi_ret_t populate_record_from_dbus(get_sdr::SensorDataFullRecordBody* body,
662 const ipmi::sensor::Info* info,
Emily Shafferbbef71c2017-05-08 16:36:17 -0700663 ipmi_data_len_t data_len)
664{
665 /* Functional sensor case */
Emily Shaffercc941e12017-06-14 13:06:26 -0700666 if (isAnalogSensor(info->propertyInterfaces.begin()->first))
Emily Shafferbbef71c2017-05-08 16:36:17 -0700667 {
Emily Shafferbbef71c2017-05-08 16:36:17 -0700668
669 body->sensor_units_1 = 0; // unsigned, no rate, no modifier, not a %
670
671 /* Unit info */
Tom Josephdc212b22018-02-16 09:59:57 +0530672 setUnitFieldsForObject(info, body);
Emily Shaffer10f49592017-05-10 12:01:10 -0700673
674 get_sdr::body::set_b(info->coefficientB, body);
675 get_sdr::body::set_m(info->coefficientM, body);
676 get_sdr::body::set_b_exp(info->exponentB, body);
Tom Josephdc212b22018-02-16 09:59:57 +0530677 get_sdr::body::set_r_exp(info->exponentR, body);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700678
Emily Shafferbbef71c2017-05-08 16:36:17 -0700679 get_sdr::body::set_id_type(0b00, body); // 00 = unicode
Emily Shafferbbef71c2017-05-08 16:36:17 -0700680 }
681
Tom Joseph96423912018-01-25 00:14:34 +0530682 /* ID string */
683 auto id_string = info->sensorNameFunc(*info);
684
685 if (id_string.length() > FULL_RECORD_ID_STR_MAX_LENGTH)
686 {
687 get_sdr::body::set_id_strlen(FULL_RECORD_ID_STR_MAX_LENGTH, body);
688 }
689 else
690 {
691 get_sdr::body::set_id_strlen(id_string.length(), body);
692 }
693 strncpy(body->id_string, id_string.c_str(),
694 get_sdr::body::get_id_strlen(body));
695
Emily Shafferbbef71c2017-05-08 16:36:17 -0700696 return IPMI_CC_OK;
697};
698
Ratan Guptae0cc8552018-01-22 14:23:04 +0530699ipmi_ret_t ipmi_fru_get_sdr(ipmi_request_t request, ipmi_response_t response,
700 ipmi_data_len_t data_len)
701{
702 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
703 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
Patrick Venture0b02be92018-08-31 11:55:55 -0700704 get_sdr::SensorDataFruRecord record{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530705 auto dataLength = 0;
706
707 auto fru = frus.begin();
Patrick Venture0b02be92018-08-31 11:55:55 -0700708 uint8_t fruID{};
Ratan Guptae0cc8552018-01-22 14:23:04 +0530709 auto recordID = get_sdr::request::get_record_id(req);
710
711 fruID = recordID - FRU_RECORD_ID_START;
712 fru = frus.find(fruID);
713 if (fru == frus.end())
714 {
715 return IPMI_CC_SENSOR_INVALID;
716 }
717
718 /* Header */
719 get_sdr::header::set_record_id(recordID, &(record.header));
720 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
721 record.header.record_type = get_sdr::SENSOR_DATA_FRU_RECORD;
722 record.header.record_length = sizeof(record.key) + sizeof(record.body);
723
724 /* Key */
725 record.key.fruID = fruID;
726 record.key.accessLun |= IPMI_LOGICAL_FRU;
727 record.key.deviceAddress = BMCSlaveAddress;
728
729 /* Body */
730 record.body.entityID = fru->second[0].entityID;
731 record.body.entityInstance = fru->second[0].entityInstance;
732 record.body.deviceType = fruInventoryDevice;
733 record.body.deviceTypeModifier = IPMIFruInventory;
734
735 /* Device ID string */
Patrick Venture0b02be92018-08-31 11:55:55 -0700736 auto deviceID =
737 fru->second[0].path.substr(fru->second[0].path.find_last_of('/') + 1,
738 fru->second[0].path.length());
Ratan Guptae0cc8552018-01-22 14:23:04 +0530739
740 if (deviceID.length() > get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH)
741 {
742 get_sdr::body::set_device_id_strlen(
Patrick Venture0b02be92018-08-31 11:55:55 -0700743 get_sdr::FRU_RECORD_DEVICE_ID_MAX_LENGTH, &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530744 }
745 else
746 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700747 get_sdr::body::set_device_id_strlen(deviceID.length(), &(record.body));
Ratan Guptae0cc8552018-01-22 14:23:04 +0530748 }
749
750 strncpy(record.body.deviceID, deviceID.c_str(),
751 get_sdr::body::get_device_id_strlen(&(record.body)));
752
753 if (++fru == frus.end())
754 {
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800755 // we have reached till end of fru, so assign the next record id to
756 // 512(Max fru ID = 511) + Entity Record ID(may start with 0).
Patrick Venture83a0b842019-07-19 18:37:15 -0700757 const auto& entityRecords = getIpmiEntityRecords();
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800758 auto next_record_id =
Patrick Venture83a0b842019-07-19 18:37:15 -0700759 (entityRecords.size())
760 ? entityRecords.begin()->first + ENTITY_RECORD_ID_START
761 : END_OF_RECORD;
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800762 get_sdr::response::set_next_record_id(next_record_id, resp);
763 }
764 else
765 {
766 get_sdr::response::set_next_record_id(
767 (FRU_RECORD_ID_START + fru->first), resp);
768 }
769
770 // Check for invalid offset size
771 if (req->offset > sizeof(record))
772 {
773 return IPMI_CC_PARM_OUT_OF_RANGE;
774 }
775
776 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
777 sizeof(record) - req->offset);
778
779 std::memcpy(resp->record_data,
780 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
781
782 *data_len = dataLength;
783 *data_len += 2; // additional 2 bytes for next record ID
784
785 return IPMI_CC_OK;
786}
787
788ipmi_ret_t ipmi_entity_get_sdr(ipmi_request_t request, ipmi_response_t response,
789 ipmi_data_len_t data_len)
790{
791 auto req = reinterpret_cast<get_sdr::GetSdrReq*>(request);
792 auto resp = reinterpret_cast<get_sdr::GetSdrResp*>(response);
793 get_sdr::SensorDataEntityRecord record{};
794 auto dataLength = 0;
795
Patrick Venture83a0b842019-07-19 18:37:15 -0700796 const auto& entityRecords = getIpmiEntityRecords();
797 auto entity = entityRecords.begin();
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800798 uint8_t entityRecordID;
799 auto recordID = get_sdr::request::get_record_id(req);
800
801 entityRecordID = recordID - ENTITY_RECORD_ID_START;
Patrick Venture83a0b842019-07-19 18:37:15 -0700802 entity = entityRecords.find(entityRecordID);
803 if (entity == entityRecords.end())
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800804 {
805 return IPMI_CC_SENSOR_INVALID;
806 }
807
808 /* Header */
809 get_sdr::header::set_record_id(recordID, &(record.header));
810 record.header.sdr_version = SDR_VERSION; // Based on IPMI Spec v2.0 rev 1.1
811 record.header.record_type = get_sdr::SENSOR_DATA_ENTITY_RECORD;
812 record.header.record_length = sizeof(record.key) + sizeof(record.body);
813
814 /* Key */
815 record.key.containerEntityId = entity->second.containerEntityId;
816 record.key.containerEntityInstance = entity->second.containerEntityInstance;
817 get_sdr::key::set_flags(entity->second.isList, entity->second.isLinked,
818 &(record.key));
819 record.key.entityId1 = entity->second.containedEntities[0].first;
820 record.key.entityInstance1 = entity->second.containedEntities[0].second;
821
822 /* Body */
823 record.body.entityId2 = entity->second.containedEntities[1].first;
824 record.body.entityInstance2 = entity->second.containedEntities[1].second;
825 record.body.entityId3 = entity->second.containedEntities[2].first;
826 record.body.entityInstance3 = entity->second.containedEntities[2].second;
827 record.body.entityId4 = entity->second.containedEntities[3].first;
828 record.body.entityInstance4 = entity->second.containedEntities[3].second;
829
Patrick Venture83a0b842019-07-19 18:37:15 -0700830 if (++entity == entityRecords.end())
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800831 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700832 get_sdr::response::set_next_record_id(END_OF_RECORD,
833 resp); // last record
Ratan Guptae0cc8552018-01-22 14:23:04 +0530834 }
835 else
836 {
837 get_sdr::response::set_next_record_id(
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800838 (ENTITY_RECORD_ID_START + entity->first), resp);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530839 }
840
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700841 // Check for invalid offset size
842 if (req->offset > sizeof(record))
Ratan Guptae0cc8552018-01-22 14:23:04 +0530843 {
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700844 return IPMI_CC_PARM_OUT_OF_RANGE;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530845 }
846
Emily Shaffer0fbdbce2018-09-27 09:30:41 -0700847 dataLength = std::min(static_cast<size_t>(req->bytes_to_read),
848 sizeof(record) - req->offset);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530849
Patrick Ventureb51bf9c2018-09-10 15:53:14 -0700850 std::memcpy(resp->record_data,
Jason M. Bills1cd85962018-10-05 12:04:01 -0700851 reinterpret_cast<uint8_t*>(&record) + req->offset, dataLength);
Ratan Guptae0cc8552018-01-22 14:23:04 +0530852
853 *data_len = dataLength;
854 *data_len += 2; // additional 2 bytes for next record ID
855
856 return IPMI_CC_OK;
857}
858
Emily Shafferbbef71c2017-05-08 16:36:17 -0700859ipmi_ret_t ipmi_sen_get_sdr(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
860 ipmi_request_t request, ipmi_response_t response,
861 ipmi_data_len_t data_len, ipmi_context_t context)
862{
863 ipmi_ret_t ret = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -0700864 get_sdr::GetSdrReq* req = (get_sdr::GetSdrReq*)request;
865 get_sdr::GetSdrResp* resp = (get_sdr::GetSdrResp*)response;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700866 get_sdr::SensorDataFullRecord record = {0};
867 if (req != NULL)
868 {
869 // Note: we use an iterator so we can provide the next ID at the end of
870 // the call.
871 auto sensor = sensors.begin();
Ratan Guptae0cc8552018-01-22 14:23:04 +0530872 auto recordID = get_sdr::request::get_record_id(req);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700873
874 // At the beginning of a scan, the host side will send us id=0.
Ratan Guptae0cc8552018-01-22 14:23:04 +0530875 if (recordID != 0)
Emily Shafferbbef71c2017-05-08 16:36:17 -0700876 {
Jaghathiswari Rankappagounder Natarajan9c118942019-02-12 13:22:55 -0800877 // recordID 0 to 255 means it is a FULL record.
878 // recordID 256 to 511 means it is a FRU record.
879 // recordID greater then 511 means it is a Entity Association
880 // record. Currently we are supporting three record types: FULL
881 // record, FRU record and Enttiy Association record.
882 if (recordID >= ENTITY_RECORD_ID_START)
883 {
884 return ipmi_entity_get_sdr(request, response, data_len);
885 }
886 else if (recordID >= FRU_RECORD_ID_START &&
887 recordID < ENTITY_RECORD_ID_START)
Ratan Guptae0cc8552018-01-22 14:23:04 +0530888 {
889 return ipmi_fru_get_sdr(request, response, data_len);
890 }
891 else
892 {
893 sensor = sensors.find(recordID);
894 if (sensor == sensors.end())
895 {
896 return IPMI_CC_SENSOR_INVALID;
897 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700898 }
899 }
900
901 uint8_t sensor_id = sensor->first;
902
903 /* Header */
904 get_sdr::header::set_record_id(sensor_id, &(record.header));
905 record.header.sdr_version = 0x51; // Based on IPMI Spec v2.0 rev 1.1
906 record.header.record_type = get_sdr::SENSOR_DATA_FULL_RECORD;
907 record.header.record_length = sizeof(get_sdr::SensorDataFullRecord);
908
909 /* Key */
Tom Joseph96423912018-01-25 00:14:34 +0530910 get_sdr::key::set_owner_id_bmc(&(record.key));
Emily Shafferbbef71c2017-05-08 16:36:17 -0700911 record.key.sensor_number = sensor_id;
912
913 /* Body */
Tom Joseph96423912018-01-25 00:14:34 +0530914 record.body.entity_id = sensor->second.entityType;
Emily Shafferbbef71c2017-05-08 16:36:17 -0700915 record.body.sensor_type = sensor->second.sensorType;
916 record.body.event_reading_type = sensor->second.sensorReadingType;
Tom Joseph96423912018-01-25 00:14:34 +0530917 record.body.entity_instance = sensor->second.instance;
Jaghathiswari Rankappagounder Natarajan0780df12019-02-06 15:29:24 -0800918 if (ipmi::sensor::Mutability::Write ==
919 (sensor->second.mutability & ipmi::sensor::Mutability::Write))
920 {
921 get_sdr::body::init_settable_state(true, &(record.body));
922 }
Emily Shafferbbef71c2017-05-08 16:36:17 -0700923
924 // Set the type-specific details given the DBus interface
925 ret = populate_record_from_dbus(&(record.body), &(sensor->second),
926 data_len);
927
928 if (++sensor == sensors.end())
929 {
Ratan Guptae0cc8552018-01-22 14:23:04 +0530930 // we have reached till end of sensor, so assign the next record id
931 // to 256(Max Sensor ID = 255) + FRU ID(may start with 0).
Patrick Venture0b02be92018-08-31 11:55:55 -0700932 auto next_record_id =
933 (frus.size()) ? frus.begin()->first + FRU_RECORD_ID_START
934 : END_OF_RECORD;
Ratan Guptae0cc8552018-01-22 14:23:04 +0530935
936 get_sdr::response::set_next_record_id(next_record_id, resp);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700937 }
938 else
939 {
940 get_sdr::response::set_next_record_id(sensor->first, resp);
941 }
942
Emily Shaffer6c9ee512018-09-27 11:04:36 -0700943 if (req->offset > sizeof(record))
944 {
945 return IPMI_CC_PARM_OUT_OF_RANGE;
946 }
947
948 // data_len will ultimately be the size of the record, plus
949 // the size of the next record ID:
950 *data_len = std::min(static_cast<size_t>(req->bytes_to_read),
951 sizeof(record) - req->offset);
952
953 std::memcpy(resp->record_data,
954 reinterpret_cast<uint8_t*>(&record) + req->offset,
955 *data_len);
956
957 // data_len should include the LSB and MSB:
Jason M. Bills1cd85962018-10-05 12:04:01 -0700958 *data_len +=
959 sizeof(resp->next_record_id_lsb) + sizeof(resp->next_record_id_msb);
Emily Shafferbbef71c2017-05-08 16:36:17 -0700960 }
961
962 return ret;
963}
964
Jia, Chunhui3342a8e2018-12-29 13:32:26 +0800965static bool isFromSystemChannel()
966{
967 // TODO we could not figure out where the request is from based on IPMI
968 // command handler parameters. because of it, we can not differentiate
969 // request from SMS/SMM or IPMB channel
970 return true;
971}
972
973ipmi_ret_t ipmicmdPlatformEvent(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
974 ipmi_request_t request,
975 ipmi_response_t response,
976 ipmi_data_len_t dataLen, ipmi_context_t context)
977{
978 uint16_t generatorID;
979 size_t count;
980 bool assert = true;
981 std::string sensorPath;
982 size_t paraLen = *dataLen;
983 PlatformEventRequest* req;
984 *dataLen = 0;
985
986 if ((paraLen < selSystemEventSizeWith1Bytes) ||
987 (paraLen > selSystemEventSizeWith3Bytes))
988 {
989 return IPMI_CC_REQ_DATA_LEN_INVALID;
990 }
991
992 if (isFromSystemChannel())
993 { // first byte for SYSTEM Interface is Generator ID
994 // +1 to get common struct
995 req = reinterpret_cast<PlatformEventRequest*>((uint8_t*)request + 1);
996 // Capture the generator ID
997 generatorID = *reinterpret_cast<uint8_t*>(request);
998 // Platform Event usually comes from other firmware, like BIOS.
999 // Unlike BMC sensor, it does not have BMC DBUS sensor path.
1000 sensorPath = "System";
1001 }
1002 else
1003 {
1004 req = reinterpret_cast<PlatformEventRequest*>(request);
1005 // TODO GenratorID for IPMB is combination of RqSA and RqLUN
1006 generatorID = 0xff;
1007 sensorPath = "IPMB";
1008 }
1009 // Content of event data field depends on sensor class.
1010 // When data0 bit[5:4] is non-zero, valid data counts is 3.
1011 // When data0 bit[7:6] is non-zero, valid data counts is 2.
1012 if (((req->data[0] & byte3EnableMask) != 0 &&
1013 paraLen < selSystemEventSizeWith3Bytes) ||
1014 ((req->data[0] & byte2EnableMask) != 0 &&
1015 paraLen < selSystemEventSizeWith2Bytes))
1016 {
1017 return IPMI_CC_REQ_DATA_LEN_INVALID;
1018 }
1019
1020 // Count bytes of Event Data
1021 if ((req->data[0] & byte3EnableMask) != 0)
1022 {
1023 count = 3;
1024 }
1025 else if ((req->data[0] & byte2EnableMask) != 0)
1026 {
1027 count = 2;
1028 }
1029 else
1030 {
1031 count = 1;
1032 }
1033 assert = req->eventDirectionType & directionMask ? false : true;
1034 std::vector<uint8_t> eventData(req->data, req->data + count);
1035
1036 sdbusplus::bus::bus dbus(bus);
1037 std::string service =
1038 ipmi::getService(dbus, ipmiSELAddInterface, ipmiSELPath);
1039 sdbusplus::message::message writeSEL = dbus.new_method_call(
1040 service.c_str(), ipmiSELPath, ipmiSELAddInterface, "IpmiSelAdd");
1041 writeSEL.append(ipmiSELAddMessage, sensorPath, eventData, assert,
1042 generatorID);
1043 try
1044 {
1045 dbus.call(writeSEL);
1046 }
1047 catch (sdbusplus::exception_t& e)
1048 {
1049 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
1050 return IPMI_CC_UNSPECIFIED_ERROR;
1051 }
1052 return IPMI_CC_OK;
1053}
1054
Chris Austenac4604a2015-10-13 12:43:27 -05001055void register_netfn_sen_functions()
1056{
Tom05732372016-09-06 17:21:23 +05301057 // <Wildcard Command>
Patrick Venture0b02be92018-08-31 11:55:55 -07001058 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_WILDCARD, nullptr,
1059 ipmi_sen_wildcard, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001060
Jia, Chunhui3342a8e2018-12-29 13:32:26 +08001061 // <Platform Event Message>
1062 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_PLATFORM_EVENT, nullptr,
1063 ipmicmdPlatformEvent, PRIVILEGE_OPERATOR);
Tom05732372016-09-06 17:21:23 +05301064 // <Get Sensor Type>
Patrick Venture0b02be92018-08-31 11:55:55 -07001065 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_TYPE, nullptr,
1066 ipmi_sen_get_sensor_type, PRIVILEGE_USER);
Chris Austenac4604a2015-10-13 12:43:27 -05001067
Tom05732372016-09-06 17:21:23 +05301068 // <Set Sensor Reading and Event Status>
Patrick Venture0b02be92018-08-31 11:55:55 -07001069 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_SET_SENSOR, nullptr,
1070 ipmi_sen_set_sensor, PRIVILEGE_OPERATOR);
Chris Austen8a45e7c2015-10-15 00:31:46 -05001071
Tom05732372016-09-06 17:21:23 +05301072 // <Get Sensor Reading>
Patrick Venture0b02be92018-08-31 11:55:55 -07001073 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_READING, nullptr,
1074 ipmi_sen_get_sensor_reading, PRIVILEGE_USER);
Emily Shaffera344afc2017-04-13 15:09:39 -07001075
Tom Joseph5ca50952018-02-22 00:33:38 +05301076 // <Reserve Device SDR Repository>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +00001077 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1078 ipmi::sensor_event::cmdReserveDeviceSdrRepository,
1079 ipmi::Privilege::User, ipmiSensorReserveSdr);
Chris Austen10ccc0f2015-12-10 18:27:04 -06001080
Tom Joseph5ca50952018-02-22 00:33:38 +05301081 // <Get Device SDR Info>
jayaprakash Mutyalad9578232019-05-13 20:22:50 +00001082 ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnSensor,
1083 ipmi::sensor_event::cmdGetDeviceSdrInfo,
1084 ipmi::Privilege::User, ipmiSensorGetDeviceSdrInfo);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001085
Tom Joseph5ca50952018-02-22 00:33:38 +05301086 // <Get Device SDR>
Patrick Venture0b02be92018-08-31 11:55:55 -07001087 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_DEVICE_SDR, nullptr,
1088 ipmi_sen_get_sdr, PRIVILEGE_USER);
Emily Shafferbbef71c2017-05-08 16:36:17 -07001089
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -06001090 // <Get Sensor Thresholds>
1091 ipmi_register_callback(NETFUN_SENSOR, IPMI_CMD_GET_SENSOR_THRESHOLDS,
1092 nullptr, ipmi_sen_get_sensor_thresholds,
1093 PRIVILEGE_USER);
1094
Chris Austenac4604a2015-10-13 12:43:27 -05001095 return;
1096}