xref: /openbmc/phosphor-host-ipmid/ipmisensor.cpp (revision 11d68897e483cfcf7c085aaa2b48a614a431508e)
146470a38SPatrick Venture #include "sensorhandler.hpp"
246470a38SPatrick Venture 
30b02be92SPatrick Venture #include <malloc.h>
40b02be92SPatrick Venture 
5391f3303SEmily Shaffer extern uint8_t find_type_for_sensor_number(uint8_t);
698a23840SMatthew Barth 
70b02be92SPatrick Venture struct sensorRES_t
80b02be92SPatrick Venture {
998a23840SMatthew Barth     uint8_t sensor_number;
1098a23840SMatthew Barth     uint8_t operation;
1198a23840SMatthew Barth     uint8_t sensor_reading;
1298a23840SMatthew Barth     uint8_t assert_state7_0;
1398a23840SMatthew Barth     uint8_t assert_state14_8;
1498a23840SMatthew Barth     uint8_t deassert_state7_0;
1598a23840SMatthew Barth     uint8_t deassert_state14_8;
1698a23840SMatthew Barth     uint8_t event_data1;
1798a23840SMatthew Barth     uint8_t event_data2;
1898a23840SMatthew Barth     uint8_t event_data3;
1998a23840SMatthew Barth } __attribute__((packed));
2098a23840SMatthew Barth 
2198a23840SMatthew Barth #define ISBITSET(x, y) (((x) >> (y)) & 0x01)
2298a23840SMatthew Barth #define ASSERTINDEX 0
2398a23840SMatthew Barth #define DEASSERTINDEX 1
2498a23840SMatthew Barth 
250b02be92SPatrick Venture // Sensor Type,  Offset, function handler, Dbus Method, Assert value, Deassert
260b02be92SPatrick Venture // value
270b02be92SPatrick Venture struct lookup_t
280b02be92SPatrick Venture {
2998a23840SMatthew Barth     uint8_t sensor_type;
3098a23840SMatthew Barth     uint8_t offset;
3198a23840SMatthew Barth     int (*func)(const sensorRES_t*, const lookup_t*, const char*);
3298a23840SMatthew Barth     char member[16];
3398a23840SMatthew Barth     char assertion[64];
3498a23840SMatthew Barth     char deassertion[64];
3598a23840SMatthew Barth };
3698a23840SMatthew Barth 
3798a23840SMatthew Barth extern int updateDbusInterface(uint8_t, const char*, const char*);
3898a23840SMatthew Barth 
390b02be92SPatrick Venture int set_sensor_dbus_state_simple(const sensorRES_t* pRec,
400b02be92SPatrick Venture                                  const lookup_t* pTable, const char* value)
410b02be92SPatrick Venture {
4298a23840SMatthew Barth 
430b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, value);
4498a23840SMatthew Barth }
4598a23840SMatthew Barth 
460b02be92SPatrick Venture struct event_data_t
470b02be92SPatrick Venture {
4898a23840SMatthew Barth     uint8_t data;
4998a23840SMatthew Barth     char text[64];
5098a23840SMatthew Barth };
5198a23840SMatthew Barth 
520b02be92SPatrick Venture event_data_t g_fwprogress02h[] = {{0x00, "Unspecified"},
5398a23840SMatthew Barth                                   {0x01, "Memory Init"},
5498a23840SMatthew Barth                                   {0x02, "HD Init"},
5598a23840SMatthew Barth                                   {0x03, "Secondary Proc Init"},
5698a23840SMatthew Barth                                   {0x04, "User Authentication"},
5798a23840SMatthew Barth                                   {0x05, "User init system setup"},
5898a23840SMatthew Barth                                   {0x06, "USB configuration"},
5998a23840SMatthew Barth                                   {0x07, "PCI configuration"},
6098a23840SMatthew Barth                                   {0x08, "Option ROM Init"},
6198a23840SMatthew Barth                                   {0x09, "Video Init"},
6298a23840SMatthew Barth                                   {0x0A, "Cache Init"},
6398a23840SMatthew Barth                                   {0x0B, "SM Bus init"},
6498a23840SMatthew Barth                                   {0x0C, "Keyboard Init"},
6598a23840SMatthew Barth                                   {0x0D, "Embedded ctrl init"},
6698a23840SMatthew Barth                                   {0x0E, "Docking station attachment"},
6798a23840SMatthew Barth                                   {0x0F, "Enable docking station"},
6898a23840SMatthew Barth                                   {0x10, "Docking station ejection"},
6998a23840SMatthew Barth                                   {0x11, "Disabling docking station"},
7098a23840SMatthew Barth                                   {0x12, "Calling OS Wakeup"},
7198a23840SMatthew Barth                                   {0x13, "Starting OS"},
7298a23840SMatthew Barth                                   {0x14, "Baseboard Init"},
7398a23840SMatthew Barth                                   {0x15, ""},
7498a23840SMatthew Barth                                   {0x16, "Floppy Init"},
7598a23840SMatthew Barth                                   {0x17, "Keyboard Test"},
7698a23840SMatthew Barth                                   {0x18, "Pointing Device Test"},
7798a23840SMatthew Barth                                   {0x19, "Primary Proc Init"},
780b02be92SPatrick Venture                                   {0xFF, "Unknown"}};
7998a23840SMatthew Barth 
8098a23840SMatthew Barth event_data_t g_fwprogress00h[] = {
8198a23840SMatthew Barth     {0x00, "Unspecified."},
8298a23840SMatthew Barth     {0x01, "No system memory detected"},
8398a23840SMatthew Barth     {0x02, "No usable system memory"},
8498a23840SMatthew Barth     {0x03, "Unrecoverable hard-disk/ATAPI/IDE"},
8598a23840SMatthew Barth     {0x04, "Unrecoverable system-board"},
8698a23840SMatthew Barth     {0x05, "Unrecoverable diskette"},
8798a23840SMatthew Barth     {0x06, "Unrecoverable hard-disk controller"},
8898a23840SMatthew Barth     {0x07, "Unrecoverable PS/2 or USB keyboard"},
8998a23840SMatthew Barth     {0x08, "Removable boot media not found"},
9098a23840SMatthew Barth     {0x09, "Unrecoverable video controller"},
9198a23840SMatthew Barth     {0x0A, "No video device detected"},
9298a23840SMatthew Barth     {0x0B, "Firmware ROM corruption detected"},
9398a23840SMatthew Barth     {0x0C, "CPU voltage mismatch"},
9498a23840SMatthew Barth     {0x0D, "CPU speed matching"},
9598a23840SMatthew Barth     {0xFF, "unknown"},
9698a23840SMatthew Barth };
9798a23840SMatthew Barth 
980b02be92SPatrick Venture char* event_data_lookup(event_data_t* p, uint8_t b)
990b02be92SPatrick Venture {
10098a23840SMatthew Barth 
1010b02be92SPatrick Venture     while (p->data != 0xFF)
1020b02be92SPatrick Venture     {
1030b02be92SPatrick Venture         if (p->data == b)
1040b02be92SPatrick Venture         {
10598a23840SMatthew Barth             break;
10698a23840SMatthew Barth         }
10798a23840SMatthew Barth         p++;
10898a23840SMatthew Barth     }
10998a23840SMatthew Barth 
11098a23840SMatthew Barth     return p->text;
11198a23840SMatthew Barth }
11298a23840SMatthew Barth 
1130b02be92SPatrick Venture //  The fw progress sensor contains some additional information that needs to be
1140b02be92SPatrick Venture //  processed prior to calling the dbus code.
1150b02be92SPatrick Venture int set_sensor_dbus_state_fwprogress(const sensorRES_t* pRec,
116*11d68897SWilly Tu                                      const lookup_t* pTable, const char*)
1170b02be92SPatrick Venture {
11898a23840SMatthew Barth 
11998a23840SMatthew Barth     char valuestring[128];
12098a23840SMatthew Barth     char* p = valuestring;
12198a23840SMatthew Barth 
1220b02be92SPatrick Venture     switch (pTable->offset)
1230b02be92SPatrick Venture     {
12498a23840SMatthew Barth 
1250b02be92SPatrick Venture         case 0x00:
126b51bf9c8SPatrick Venture             std::snprintf(
127b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "POST Error, %s",
1280b02be92SPatrick Venture                 event_data_lookup(g_fwprogress00h, pRec->event_data2));
12998a23840SMatthew Barth             break;
1300b02be92SPatrick Venture         case 0x01: /* Using g_fwprogress02h for 0x01 because that's what the
1310b02be92SPatrick Venture                       ipmi spec says to do */
132b51bf9c8SPatrick Venture             std::snprintf(
133b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "FW Hang, %s",
1340b02be92SPatrick Venture                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
13598a23840SMatthew Barth             break;
1360b02be92SPatrick Venture         case 0x02:
137b51bf9c8SPatrick Venture             std::snprintf(
138b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "FW Progress, %s",
1390b02be92SPatrick Venture                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
14098a23840SMatthew Barth             break;
1410b02be92SPatrick Venture         default:
142b51bf9c8SPatrick Venture             std::snprintf(
143b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1440b02be92SPatrick Venture                 "Internal warning, fw_progres offset unknown (0x%02x)",
1450b02be92SPatrick Venture                 pTable->offset);
14698a23840SMatthew Barth             break;
14798a23840SMatthew Barth     }
14898a23840SMatthew Barth 
1490b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
15098a23840SMatthew Barth }
15198a23840SMatthew Barth 
1520b02be92SPatrick Venture // Handling this special OEM sensor by coping what is in byte 4.  I also think
1530b02be92SPatrick Venture // that is odd considering byte 3 is for sensor reading.  This seems like a
1540b02be92SPatrick Venture // misuse of the IPMI spec
155*11d68897SWilly Tu int set_sensor_dbus_state_osbootcount(const sensorRES_t* pRec, const lookup_t*,
156*11d68897SWilly Tu                                       const char*)
1570b02be92SPatrick Venture {
1580b02be92SPatrick Venture     return set_sensor_dbus_state_y(pRec->sensor_number, "setValue",
15998a23840SMatthew Barth                                    pRec->assert_state7_0);
16098a23840SMatthew Barth }
16198a23840SMatthew Barth 
1620b02be92SPatrick Venture int set_sensor_dbus_state_system_event(const sensorRES_t* pRec,
163*11d68897SWilly Tu                                        const lookup_t* pTable, const char*)
1640b02be92SPatrick Venture {
16598a23840SMatthew Barth     char valuestring[128];
16698a23840SMatthew Barth     char* p = valuestring;
16798a23840SMatthew Barth 
1680b02be92SPatrick Venture     switch (pTable->offset)
1690b02be92SPatrick Venture     {
17098a23840SMatthew Barth 
1710b02be92SPatrick Venture         case 0x00:
172b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring), "System Reconfigured");
17398a23840SMatthew Barth             break;
1740b02be92SPatrick Venture         case 0x01:
175b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring), "OEM Boot Event");
17698a23840SMatthew Barth             break;
1770b02be92SPatrick Venture         case 0x02:
178b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring),
1790b02be92SPatrick Venture                           "Undetermined System Hardware Failure");
18098a23840SMatthew Barth             break;
1810b02be92SPatrick Venture         case 0x03:
182b51bf9c8SPatrick Venture             std::snprintf(
183b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1840b02be92SPatrick Venture                 "System Failure see error log for more details (0x%02x)",
1850b02be92SPatrick Venture                 pRec->event_data2);
18698a23840SMatthew Barth             break;
1870b02be92SPatrick Venture         case 0x04:
188b51bf9c8SPatrick Venture             std::snprintf(
1890b02be92SPatrick Venture                 p, sizeof(valuestring),
1900b02be92SPatrick Venture                 "System Failure see PEF error log for more details (0x%02x)",
1910b02be92SPatrick Venture                 pRec->event_data2);
19298a23840SMatthew Barth             break;
1930b02be92SPatrick Venture         default:
194b51bf9c8SPatrick Venture             std::snprintf(
195b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1960b02be92SPatrick Venture                 "Internal warning, system_event offset unknown (0x%02x)",
1970b02be92SPatrick Venture                 pTable->offset);
19898a23840SMatthew Barth             break;
19998a23840SMatthew Barth     }
20098a23840SMatthew Barth 
2010b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
20298a23840SMatthew Barth }
20398a23840SMatthew Barth 
20498a23840SMatthew Barth //  This table lists only senors we care about telling dbus about.
20598a23840SMatthew Barth //  Offset definition cab be found in section 42.2 of the IPMI 2.0
20698a23840SMatthew Barth //  spec.  Add more if/when there are more items of interest.
20798a23840SMatthew Barth lookup_t g_ipmidbuslookup[] = {
20898a23840SMatthew Barth 
2090b02be92SPatrick Venture     {0xe9, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled",
2100b02be92SPatrick Venture      ""}, // OCC Inactive 0
2110b02be92SPatrick Venture     {0xe9, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled",
2120b02be92SPatrick Venture      ""}, // OCC Active 1
2130661beb1SJayanth Othayoth     // Turbo Allowed
21400f0815bSTom Joseph     {0xda, 0x00, set_sensor_dbus_state_simple, "setValue", "True", "False"},
215d5b2ac0eSJayanth Othayoth     // Power Supply Derating
216d5b2ac0eSJayanth Othayoth     {0xb4, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
2175422e0e9SJayanth Othayoth     // Power Cap
2185422e0e9SJayanth Othayoth     {0xC2, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
21998a23840SMatthew Barth     {0x07, 0x07, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
22098a23840SMatthew Barth     {0x07, 0x08, set_sensor_dbus_state_simple, "setFault", "True", "False"},
22198a23840SMatthew Barth     {0x0C, 0x06, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
22298a23840SMatthew Barth     {0x0C, 0x04, set_sensor_dbus_state_simple, "setFault", "True", "False"},
22398a23840SMatthew Barth     {0x0F, 0x02, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22498a23840SMatthew Barth     {0x0F, 0x01, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22598a23840SMatthew Barth     {0x0F, 0x00, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22698a23840SMatthew Barth     {0xC7, 0x01, set_sensor_dbus_state_simple, "setFault", "True", "False"},
22798a23840SMatthew Barth     {0xc3, 0x00, set_sensor_dbus_state_osbootcount, "setValue", "", ""},
2280b02be92SPatrick Venture     {0x1F, 0x00, set_sensor_dbus_state_simple, "setValue",
2290b02be92SPatrick Venture      "Boot completed (00)", ""},
2300b02be92SPatrick Venture     {0x1F, 0x01, set_sensor_dbus_state_simple, "setValue",
2310b02be92SPatrick Venture      "Boot completed (01)", ""},
2320b02be92SPatrick Venture     {0x1F, 0x02, set_sensor_dbus_state_simple, "setValue", "PXE boot completed",
2330b02be92SPatrick Venture      ""},
2340b02be92SPatrick Venture     {0x1F, 0x03, set_sensor_dbus_state_simple, "setValue",
2350b02be92SPatrick Venture      "Diagnostic boot completed", ""},
2360b02be92SPatrick Venture     {0x1F, 0x04, set_sensor_dbus_state_simple, "setValue",
2370b02be92SPatrick Venture      "CD-ROM boot completed", ""},
2380b02be92SPatrick Venture     {0x1F, 0x05, set_sensor_dbus_state_simple, "setValue", "ROM boot completed",
2390b02be92SPatrick Venture      ""},
2400b02be92SPatrick Venture     {0x1F, 0x06, set_sensor_dbus_state_simple, "setValue",
2410b02be92SPatrick Venture      "Boot completed (06)", ""},
24298a23840SMatthew Barth     {0x12, 0x00, set_sensor_dbus_state_system_event, "setValue", "", ""},
24398a23840SMatthew Barth     {0x12, 0x01, set_sensor_dbus_state_system_event, "setValue", "", ""},
24498a23840SMatthew Barth     {0x12, 0x02, set_sensor_dbus_state_system_event, "setValue", "", ""},
24598a23840SMatthew Barth     {0x12, 0x03, set_sensor_dbus_state_system_event, "setValue", "", ""},
24698a23840SMatthew Barth     {0x12, 0x04, set_sensor_dbus_state_system_event, "setValue", "", ""},
2476244f93dSDhruvaraj Subhashchandran     {0xCA, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled", ""},
2486244f93dSDhruvaraj Subhashchandran     {0xCA, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled", ""},
2490b02be92SPatrick Venture     {0xFF, 0xFF, NULL, "", "", ""}};
25098a23840SMatthew Barth 
251d99148b3SPatrick Venture void reportSensorEventAssert(const sensorRES_t* pRec, int index)
2520b02be92SPatrick Venture {
25398a23840SMatthew Barth     lookup_t* pTable = &g_ipmidbuslookup[index];
25498a23840SMatthew Barth     (*pTable->func)(pRec, pTable, pTable->assertion);
25598a23840SMatthew Barth }
256d99148b3SPatrick Venture void reportSensorEventDeassert(const sensorRES_t* pRec, int index)
2570b02be92SPatrick Venture {
25898a23840SMatthew Barth     lookup_t* pTable = &g_ipmidbuslookup[index];
25998a23840SMatthew Barth     (*pTable->func)(pRec, pTable, pTable->deassertion);
26098a23840SMatthew Barth }
26198a23840SMatthew Barth 
2620b02be92SPatrick Venture int findindex(const uint8_t sensor_type, int offset, int* index)
2630b02be92SPatrick Venture {
26498a23840SMatthew Barth 
26598a23840SMatthew Barth     int i = 0, rc = 0;
26698a23840SMatthew Barth     lookup_t* pTable = g_ipmidbuslookup;
26798a23840SMatthew Barth 
2680b02be92SPatrick Venture     do
2690b02be92SPatrick Venture     {
27098a23840SMatthew Barth         if (((pTable + i)->sensor_type == sensor_type) &&
2710b02be92SPatrick Venture             ((pTable + i)->offset == offset))
2720b02be92SPatrick Venture         {
27398a23840SMatthew Barth             rc = 1;
27498a23840SMatthew Barth             *index = i;
27598a23840SMatthew Barth             break;
27698a23840SMatthew Barth         }
27798a23840SMatthew Barth         i++;
27898a23840SMatthew Barth     } while ((pTable + i)->sensor_type != 0xFF);
27998a23840SMatthew Barth 
28098a23840SMatthew Barth     return rc;
28198a23840SMatthew Barth }
28298a23840SMatthew Barth 
2830b02be92SPatrick Venture bool shouldReport(uint8_t sensorType, int offset, int* index)
2840b02be92SPatrick Venture {
28598a23840SMatthew Barth     bool rc = false;
28698a23840SMatthew Barth 
2870b02be92SPatrick Venture     if (findindex(sensorType, offset, index))
2880b02be92SPatrick Venture     {
2890b02be92SPatrick Venture         rc = true;
2900b02be92SPatrick Venture     }
2910b02be92SPatrick Venture     if (rc == false)
2920b02be92SPatrick Venture     {
2935fb14603SAditya Saripalli #ifdef __IPMI_DEBUG__
2945fb14603SAditya Saripalli         log<level::DEBUG>("LOOKATME: Sensor should not be reported",
2955fb14603SAditya Saripalli                           entry("SENSORTYPE=0x%02x", sensorType),
2965fb14603SAditya Saripalli                           entry("OFFSET=0x%02x", offset));
2975fb14603SAditya Saripalli #endif
2985fb14603SAditya Saripalli     }
29998a23840SMatthew Barth 
30098a23840SMatthew Barth     return rc;
30198a23840SMatthew Barth }
30298a23840SMatthew Barth 
3030b02be92SPatrick Venture int updateSensorRecordFromSSRAESC(const void* record)
3040b02be92SPatrick Venture {
305d99148b3SPatrick Venture     auto pRec = static_cast<const sensorRES_t*>(record);
30698a23840SMatthew Barth     uint8_t stype;
3074491a46fSPatrick Venture     int index;
30898a23840SMatthew Barth 
309391f3303SEmily Shaffer     stype = find_type_for_sensor_number(pRec->sensor_number);
31098a23840SMatthew Barth 
31198a23840SMatthew Barth     // 0xC3 types use the assertion7_0 for the value to be set
31298a23840SMatthew Barth     // so skip the reseach and call the correct event reporting
31398a23840SMatthew Barth     // function
3140b02be92SPatrick Venture     if (stype == 0xC3)
3150b02be92SPatrick Venture     {
31698a23840SMatthew Barth         shouldReport(stype, 0x00, &index);
31798a23840SMatthew Barth         reportSensorEventAssert(pRec, index);
3180b02be92SPatrick Venture     }
3190b02be92SPatrick Venture     else
3200b02be92SPatrick Venture     {
32198a23840SMatthew Barth         // Scroll through each bit position .  Determine
32298a23840SMatthew Barth         // if any bit is either asserted or Deasserted.
3234491a46fSPatrick Venture         for (int i = 0; i < 8; i++)
3240b02be92SPatrick Venture         {
32598a23840SMatthew Barth 
32698a23840SMatthew Barth             if ((ISBITSET(pRec->assert_state7_0, i)) &&
32798a23840SMatthew Barth                 (shouldReport(stype, i, &index)))
32898a23840SMatthew Barth             {
32998a23840SMatthew Barth                 reportSensorEventAssert(pRec, index);
33098a23840SMatthew Barth             }
33198a23840SMatthew Barth             if ((ISBITSET(pRec->assert_state14_8, i)) &&
33298a23840SMatthew Barth                 (shouldReport(stype, i + 8, &index)))
33398a23840SMatthew Barth             {
33498a23840SMatthew Barth                 reportSensorEventAssert(pRec, index);
33598a23840SMatthew Barth             }
33698a23840SMatthew Barth             if ((ISBITSET(pRec->deassert_state7_0, i)) &&
33798a23840SMatthew Barth                 (shouldReport(stype, i, &index)))
33898a23840SMatthew Barth             {
33998a23840SMatthew Barth                 reportSensorEventDeassert(pRec, index);
34098a23840SMatthew Barth             }
34198a23840SMatthew Barth             if ((ISBITSET(pRec->deassert_state14_8, i)) &&
34298a23840SMatthew Barth                 (shouldReport(stype, i + 8, &index)))
34398a23840SMatthew Barth             {
34498a23840SMatthew Barth                 reportSensorEventDeassert(pRec, index);
34598a23840SMatthew Barth             }
34698a23840SMatthew Barth         }
34798a23840SMatthew Barth     }
34898a23840SMatthew Barth 
34998a23840SMatthew Barth     return 0;
35098a23840SMatthew Barth }
351