xref: /openbmc/phosphor-host-ipmid/ipmisensor.cpp (revision 1318a5ed36cfd41335e687b54db1c17c0dde8f45)
146470a38SPatrick Venture #include "sensorhandler.hpp"
246470a38SPatrick Venture 
30b02be92SPatrick Venture #include <malloc.h>
40b02be92SPatrick Venture 
55e72ce99SGeorge Liu #include <phosphor-logging/lg2.hpp>
65e72ce99SGeorge Liu 
7391f3303SEmily Shaffer extern uint8_t find_type_for_sensor_number(uint8_t);
898a23840SMatthew Barth 
90b02be92SPatrick Venture struct sensorRES_t
100b02be92SPatrick Venture {
1198a23840SMatthew Barth     uint8_t sensor_number;
1298a23840SMatthew Barth     uint8_t operation;
1398a23840SMatthew Barth     uint8_t sensor_reading;
1498a23840SMatthew Barth     uint8_t assert_state7_0;
1598a23840SMatthew Barth     uint8_t assert_state14_8;
1698a23840SMatthew Barth     uint8_t deassert_state7_0;
1798a23840SMatthew Barth     uint8_t deassert_state14_8;
1898a23840SMatthew Barth     uint8_t event_data1;
1998a23840SMatthew Barth     uint8_t event_data2;
2098a23840SMatthew Barth     uint8_t event_data3;
2198a23840SMatthew Barth } __attribute__((packed));
2298a23840SMatthew Barth 
2398a23840SMatthew Barth #define ISBITSET(x, y) (((x) >> (y)) & 0x01)
2498a23840SMatthew Barth #define ASSERTINDEX 0
2598a23840SMatthew Barth #define DEASSERTINDEX 1
2698a23840SMatthew Barth 
270b02be92SPatrick Venture // Sensor Type,  Offset, function handler, Dbus Method, Assert value, Deassert
280b02be92SPatrick Venture // value
290b02be92SPatrick Venture struct lookup_t
300b02be92SPatrick Venture {
3198a23840SMatthew Barth     uint8_t sensor_type;
3298a23840SMatthew Barth     uint8_t offset;
3398a23840SMatthew Barth     int (*func)(const sensorRES_t*, const lookup_t*, const char*);
3498a23840SMatthew Barth     char member[16];
3598a23840SMatthew Barth     char assertion[64];
3698a23840SMatthew Barth     char deassertion[64];
3798a23840SMatthew Barth };
3898a23840SMatthew Barth 
3998a23840SMatthew Barth extern int updateDbusInterface(uint8_t, const char*, const char*);
4098a23840SMatthew Barth 
set_sensor_dbus_state_simple(const sensorRES_t * pRec,const lookup_t * pTable,const char * value)410b02be92SPatrick Venture int set_sensor_dbus_state_simple(const sensorRES_t* pRec,
420b02be92SPatrick Venture                                  const lookup_t* pTable, const char* value)
430b02be92SPatrick Venture {
440b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, value);
4598a23840SMatthew Barth }
4698a23840SMatthew Barth 
470b02be92SPatrick Venture struct event_data_t
480b02be92SPatrick Venture {
4998a23840SMatthew Barth     uint8_t data;
5098a23840SMatthew Barth     char text[64];
5198a23840SMatthew Barth };
5298a23840SMatthew Barth 
53*1318a5edSPatrick Williams event_data_t g_fwprogress02h[] = {
54*1318a5edSPatrick Williams     {0x00, "Unspecified"},
5598a23840SMatthew Barth     {0x01, "Memory Init"},
5698a23840SMatthew Barth     {0x02, "HD Init"},
5798a23840SMatthew Barth     {0x03, "Secondary Proc Init"},
5898a23840SMatthew Barth     {0x04, "User Authentication"},
5998a23840SMatthew Barth     {0x05, "User init system setup"},
6098a23840SMatthew Barth     {0x06, "USB configuration"},
6198a23840SMatthew Barth     {0x07, "PCI configuration"},
6298a23840SMatthew Barth     {0x08, "Option ROM Init"},
6398a23840SMatthew Barth     {0x09, "Video Init"},
6498a23840SMatthew Barth     {0x0A, "Cache Init"},
6598a23840SMatthew Barth     {0x0B, "SM Bus init"},
6698a23840SMatthew Barth     {0x0C, "Keyboard Init"},
6798a23840SMatthew Barth     {0x0D, "Embedded ctrl init"},
6898a23840SMatthew Barth     {0x0E, "Docking station attachment"},
6998a23840SMatthew Barth     {0x0F, "Enable docking station"},
7098a23840SMatthew Barth     {0x10, "Docking station ejection"},
7198a23840SMatthew Barth     {0x11, "Disabling docking station"},
7298a23840SMatthew Barth     {0x12, "Calling OS Wakeup"},
7398a23840SMatthew Barth     {0x13, "Starting OS"},
7498a23840SMatthew Barth     {0x14, "Baseboard Init"},
7598a23840SMatthew Barth     {0x15, ""},
7698a23840SMatthew Barth     {0x16, "Floppy Init"},
7798a23840SMatthew Barth     {0x17, "Keyboard Test"},
7898a23840SMatthew Barth     {0x18, "Pointing Device Test"},
7998a23840SMatthew Barth     {0x19, "Primary Proc Init"},
800b02be92SPatrick Venture     {0xFF, "Unknown"}};
8198a23840SMatthew Barth 
8298a23840SMatthew Barth event_data_t g_fwprogress00h[] = {
8398a23840SMatthew Barth     {0x00, "Unspecified."},
8498a23840SMatthew Barth     {0x01, "No system memory detected"},
8598a23840SMatthew Barth     {0x02, "No usable system memory"},
8698a23840SMatthew Barth     {0x03, "Unrecoverable hard-disk/ATAPI/IDE"},
8798a23840SMatthew Barth     {0x04, "Unrecoverable system-board"},
8898a23840SMatthew Barth     {0x05, "Unrecoverable diskette"},
8998a23840SMatthew Barth     {0x06, "Unrecoverable hard-disk controller"},
9098a23840SMatthew Barth     {0x07, "Unrecoverable PS/2 or USB keyboard"},
9198a23840SMatthew Barth     {0x08, "Removable boot media not found"},
9298a23840SMatthew Barth     {0x09, "Unrecoverable video controller"},
9398a23840SMatthew Barth     {0x0A, "No video device detected"},
9498a23840SMatthew Barth     {0x0B, "Firmware ROM corruption detected"},
9598a23840SMatthew Barth     {0x0C, "CPU voltage mismatch"},
9698a23840SMatthew Barth     {0x0D, "CPU speed matching"},
9798a23840SMatthew Barth     {0xFF, "unknown"},
9898a23840SMatthew Barth };
9998a23840SMatthew Barth 
event_data_lookup(event_data_t * p,uint8_t b)1000b02be92SPatrick Venture char* event_data_lookup(event_data_t* p, uint8_t b)
1010b02be92SPatrick Venture {
1020b02be92SPatrick Venture     while (p->data != 0xFF)
1030b02be92SPatrick Venture     {
1040b02be92SPatrick Venture         if (p->data == b)
1050b02be92SPatrick Venture         {
10698a23840SMatthew Barth             break;
10798a23840SMatthew Barth         }
10898a23840SMatthew Barth         p++;
10998a23840SMatthew Barth     }
11098a23840SMatthew Barth 
11198a23840SMatthew Barth     return p->text;
11298a23840SMatthew Barth }
11398a23840SMatthew Barth 
1140b02be92SPatrick Venture //  The fw progress sensor contains some additional information that needs to be
1150b02be92SPatrick Venture //  processed prior to calling the dbus code.
set_sensor_dbus_state_fwprogress(const sensorRES_t * pRec,const lookup_t * pTable,const char *)1160b02be92SPatrick Venture int set_sensor_dbus_state_fwprogress(const sensorRES_t* pRec,
11711d68897SWilly Tu                                      const lookup_t* pTable, const char*)
1180b02be92SPatrick Venture {
11998a23840SMatthew Barth     char valuestring[128];
12098a23840SMatthew Barth     char* p = valuestring;
12198a23840SMatthew Barth 
1220b02be92SPatrick Venture     switch (pTable->offset)
1230b02be92SPatrick Venture     {
1240b02be92SPatrick Venture         case 0x00:
125b51bf9c8SPatrick Venture             std::snprintf(
126b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "POST Error, %s",
1270b02be92SPatrick Venture                 event_data_lookup(g_fwprogress00h, pRec->event_data2));
12898a23840SMatthew Barth             break;
1290b02be92SPatrick Venture         case 0x01: /* Using g_fwprogress02h for 0x01 because that's what the
1300b02be92SPatrick Venture                       ipmi spec says to do */
131b51bf9c8SPatrick Venture             std::snprintf(
132b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "FW Hang, %s",
1330b02be92SPatrick Venture                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
13498a23840SMatthew Barth             break;
1350b02be92SPatrick Venture         case 0x02:
136b51bf9c8SPatrick Venture             std::snprintf(
137b51bf9c8SPatrick Venture                 p, sizeof(valuestring), "FW Progress, %s",
1380b02be92SPatrick Venture                 event_data_lookup(g_fwprogress02h, pRec->event_data2));
13998a23840SMatthew Barth             break;
1400b02be92SPatrick Venture         default:
141b51bf9c8SPatrick Venture             std::snprintf(
142b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1430b02be92SPatrick Venture                 "Internal warning, fw_progres offset unknown (0x%02x)",
1440b02be92SPatrick Venture                 pTable->offset);
14598a23840SMatthew Barth             break;
14698a23840SMatthew Barth     }
14798a23840SMatthew Barth 
1480b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
14998a23840SMatthew Barth }
15098a23840SMatthew Barth 
1510b02be92SPatrick Venture // Handling this special OEM sensor by coping what is in byte 4.  I also think
1520b02be92SPatrick Venture // that is odd considering byte 3 is for sensor reading.  This seems like a
1530b02be92SPatrick Venture // misuse of the IPMI spec
set_sensor_dbus_state_osbootcount(const sensorRES_t * pRec,const lookup_t *,const char *)15411d68897SWilly Tu int set_sensor_dbus_state_osbootcount(const sensorRES_t* pRec, const lookup_t*,
15511d68897SWilly Tu                                       const char*)
1560b02be92SPatrick Venture {
1570b02be92SPatrick Venture     return set_sensor_dbus_state_y(pRec->sensor_number, "setValue",
15898a23840SMatthew Barth                                    pRec->assert_state7_0);
15998a23840SMatthew Barth }
16098a23840SMatthew Barth 
set_sensor_dbus_state_system_event(const sensorRES_t * pRec,const lookup_t * pTable,const char *)1610b02be92SPatrick Venture int set_sensor_dbus_state_system_event(const sensorRES_t* pRec,
16211d68897SWilly Tu                                        const lookup_t* pTable, const char*)
1630b02be92SPatrick Venture {
16498a23840SMatthew Barth     char valuestring[128];
16598a23840SMatthew Barth     char* p = valuestring;
16698a23840SMatthew Barth 
1670b02be92SPatrick Venture     switch (pTable->offset)
1680b02be92SPatrick Venture     {
1690b02be92SPatrick Venture         case 0x00:
170b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring), "System Reconfigured");
17198a23840SMatthew Barth             break;
1720b02be92SPatrick Venture         case 0x01:
173b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring), "OEM Boot Event");
17498a23840SMatthew Barth             break;
1750b02be92SPatrick Venture         case 0x02:
176b51bf9c8SPatrick Venture             std::snprintf(p, sizeof(valuestring),
1770b02be92SPatrick Venture                           "Undetermined System Hardware Failure");
17898a23840SMatthew Barth             break;
1790b02be92SPatrick Venture         case 0x03:
180b51bf9c8SPatrick Venture             std::snprintf(
181b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1820b02be92SPatrick Venture                 "System Failure see error log for more details (0x%02x)",
1830b02be92SPatrick Venture                 pRec->event_data2);
18498a23840SMatthew Barth             break;
1850b02be92SPatrick Venture         case 0x04:
186b51bf9c8SPatrick Venture             std::snprintf(
1870b02be92SPatrick Venture                 p, sizeof(valuestring),
1880b02be92SPatrick Venture                 "System Failure see PEF error log for more details (0x%02x)",
1890b02be92SPatrick Venture                 pRec->event_data2);
19098a23840SMatthew Barth             break;
1910b02be92SPatrick Venture         default:
192b51bf9c8SPatrick Venture             std::snprintf(
193b51bf9c8SPatrick Venture                 p, sizeof(valuestring),
1940b02be92SPatrick Venture                 "Internal warning, system_event offset unknown (0x%02x)",
1950b02be92SPatrick Venture                 pTable->offset);
19698a23840SMatthew Barth             break;
19798a23840SMatthew Barth     }
19898a23840SMatthew Barth 
1990b02be92SPatrick Venture     return set_sensor_dbus_state_s(pRec->sensor_number, pTable->member, p);
20098a23840SMatthew Barth }
20198a23840SMatthew Barth 
20298a23840SMatthew Barth //  This table lists only senors we care about telling dbus about.
20398a23840SMatthew Barth //  Offset definition cab be found in section 42.2 of the IPMI 2.0
20498a23840SMatthew Barth //  spec.  Add more if/when there are more items of interest.
20598a23840SMatthew Barth lookup_t g_ipmidbuslookup[] = {
20698a23840SMatthew Barth 
2070b02be92SPatrick Venture     {0xe9, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled",
2080b02be92SPatrick Venture      ""}, // OCC Inactive 0
2090b02be92SPatrick Venture     {0xe9, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled",
2100b02be92SPatrick Venture      ""}, // OCC Active 1
2110661beb1SJayanth Othayoth     // Turbo Allowed
21200f0815bSTom Joseph     {0xda, 0x00, set_sensor_dbus_state_simple, "setValue", "True", "False"},
213d5b2ac0eSJayanth Othayoth     // Power Supply Derating
214d5b2ac0eSJayanth Othayoth     {0xb4, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
2155422e0e9SJayanth Othayoth     // Power Cap
2165422e0e9SJayanth Othayoth     {0xC2, 0x00, set_sensor_dbus_state_simple, "setValue", "", ""},
21798a23840SMatthew Barth     {0x07, 0x07, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
21898a23840SMatthew Barth     {0x07, 0x08, set_sensor_dbus_state_simple, "setFault", "True", "False"},
21998a23840SMatthew Barth     {0x0C, 0x06, set_sensor_dbus_state_simple, "setPresent", "True", "False"},
22098a23840SMatthew Barth     {0x0C, 0x04, set_sensor_dbus_state_simple, "setFault", "True", "False"},
22198a23840SMatthew Barth     {0x0F, 0x02, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22298a23840SMatthew Barth     {0x0F, 0x01, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22398a23840SMatthew Barth     {0x0F, 0x00, set_sensor_dbus_state_fwprogress, "setValue", "True", "False"},
22498a23840SMatthew Barth     {0xC7, 0x01, set_sensor_dbus_state_simple, "setFault", "True", "False"},
22598a23840SMatthew Barth     {0xc3, 0x00, set_sensor_dbus_state_osbootcount, "setValue", "", ""},
2260b02be92SPatrick Venture     {0x1F, 0x00, set_sensor_dbus_state_simple, "setValue",
2270b02be92SPatrick Venture      "Boot completed (00)", ""},
2280b02be92SPatrick Venture     {0x1F, 0x01, set_sensor_dbus_state_simple, "setValue",
2290b02be92SPatrick Venture      "Boot completed (01)", ""},
2300b02be92SPatrick Venture     {0x1F, 0x02, set_sensor_dbus_state_simple, "setValue", "PXE boot completed",
2310b02be92SPatrick Venture      ""},
2320b02be92SPatrick Venture     {0x1F, 0x03, set_sensor_dbus_state_simple, "setValue",
2330b02be92SPatrick Venture      "Diagnostic boot completed", ""},
2340b02be92SPatrick Venture     {0x1F, 0x04, set_sensor_dbus_state_simple, "setValue",
2350b02be92SPatrick Venture      "CD-ROM boot completed", ""},
2360b02be92SPatrick Venture     {0x1F, 0x05, set_sensor_dbus_state_simple, "setValue", "ROM boot completed",
2370b02be92SPatrick Venture      ""},
2380b02be92SPatrick Venture     {0x1F, 0x06, set_sensor_dbus_state_simple, "setValue",
2390b02be92SPatrick Venture      "Boot completed (06)", ""},
24098a23840SMatthew Barth     {0x12, 0x00, set_sensor_dbus_state_system_event, "setValue", "", ""},
24198a23840SMatthew Barth     {0x12, 0x01, set_sensor_dbus_state_system_event, "setValue", "", ""},
24298a23840SMatthew Barth     {0x12, 0x02, set_sensor_dbus_state_system_event, "setValue", "", ""},
24398a23840SMatthew Barth     {0x12, 0x03, set_sensor_dbus_state_system_event, "setValue", "", ""},
24498a23840SMatthew Barth     {0x12, 0x04, set_sensor_dbus_state_system_event, "setValue", "", ""},
2456244f93dSDhruvaraj Subhashchandran     {0xCA, 0x00, set_sensor_dbus_state_simple, "setValue", "Disabled", ""},
2466244f93dSDhruvaraj Subhashchandran     {0xCA, 0x01, set_sensor_dbus_state_simple, "setValue", "Enabled", ""},
2470b02be92SPatrick Venture     {0xFF, 0xFF, NULL, "", "", ""}};
24898a23840SMatthew Barth 
reportSensorEventAssert(const sensorRES_t * pRec,int index)249d99148b3SPatrick Venture void reportSensorEventAssert(const sensorRES_t* pRec, int index)
2500b02be92SPatrick Venture {
25198a23840SMatthew Barth     lookup_t* pTable = &g_ipmidbuslookup[index];
25298a23840SMatthew Barth     (*pTable->func)(pRec, pTable, pTable->assertion);
25398a23840SMatthew Barth }
reportSensorEventDeassert(const sensorRES_t * pRec,int index)254d99148b3SPatrick Venture void reportSensorEventDeassert(const sensorRES_t* pRec, int index)
2550b02be92SPatrick Venture {
25698a23840SMatthew Barth     lookup_t* pTable = &g_ipmidbuslookup[index];
25798a23840SMatthew Barth     (*pTable->func)(pRec, pTable, pTable->deassertion);
25898a23840SMatthew Barth }
25998a23840SMatthew Barth 
findindex(const uint8_t sensor_type,int offset,int * index)2600b02be92SPatrick Venture int findindex(const uint8_t sensor_type, int offset, int* index)
2610b02be92SPatrick Venture {
26298a23840SMatthew Barth     int i = 0, rc = 0;
26398a23840SMatthew Barth     lookup_t* pTable = g_ipmidbuslookup;
26498a23840SMatthew Barth 
2650b02be92SPatrick Venture     do
2660b02be92SPatrick Venture     {
26798a23840SMatthew Barth         if (((pTable + i)->sensor_type == sensor_type) &&
2680b02be92SPatrick Venture             ((pTable + i)->offset == offset))
2690b02be92SPatrick Venture         {
27098a23840SMatthew Barth             rc = 1;
27198a23840SMatthew Barth             *index = i;
27298a23840SMatthew Barth             break;
27398a23840SMatthew Barth         }
27498a23840SMatthew Barth         i++;
27598a23840SMatthew Barth     } while ((pTable + i)->sensor_type != 0xFF);
27698a23840SMatthew Barth 
27798a23840SMatthew Barth     return rc;
27898a23840SMatthew Barth }
27998a23840SMatthew Barth 
shouldReport(uint8_t sensorType,int offset,int * index)2800b02be92SPatrick Venture bool shouldReport(uint8_t sensorType, int offset, int* index)
2810b02be92SPatrick Venture {
28298a23840SMatthew Barth     bool rc = false;
28398a23840SMatthew Barth 
2840b02be92SPatrick Venture     if (findindex(sensorType, offset, index))
2850b02be92SPatrick Venture     {
2860b02be92SPatrick Venture         rc = true;
2870b02be92SPatrick Venture     }
2880b02be92SPatrick Venture     if (rc == false)
2890b02be92SPatrick Venture     {
2905fb14603SAditya Saripalli #ifdef __IPMI_DEBUG__
2915e72ce99SGeorge Liu         lg2::debug("LOOKATME: Sensor should not be reported, "
2925e72ce99SGeorge Liu                    "sensor type: {SENSORTYPE}, offset: {OFFSET}",
2935e72ce99SGeorge Liu                    SENSORTYPE, lg2::hex, sensorType, "OFFSET", lg2::hex,
2945e72ce99SGeorge Liu                    offset);
2955fb14603SAditya Saripalli #endif
2965fb14603SAditya Saripalli     }
29798a23840SMatthew Barth 
29898a23840SMatthew Barth     return rc;
29998a23840SMatthew Barth }
30098a23840SMatthew Barth 
updateSensorRecordFromSSRAESC(const void * record)3010b02be92SPatrick Venture int updateSensorRecordFromSSRAESC(const void* record)
3020b02be92SPatrick Venture {
303d99148b3SPatrick Venture     auto pRec = static_cast<const sensorRES_t*>(record);
30498a23840SMatthew Barth     uint8_t stype;
3054491a46fSPatrick Venture     int index;
30698a23840SMatthew Barth 
307391f3303SEmily Shaffer     stype = find_type_for_sensor_number(pRec->sensor_number);
30898a23840SMatthew Barth 
30998a23840SMatthew Barth     // 0xC3 types use the assertion7_0 for the value to be set
31098a23840SMatthew Barth     // so skip the reseach and call the correct event reporting
31198a23840SMatthew Barth     // function
3120b02be92SPatrick Venture     if (stype == 0xC3)
3130b02be92SPatrick Venture     {
31498a23840SMatthew Barth         shouldReport(stype, 0x00, &index);
31598a23840SMatthew Barth         reportSensorEventAssert(pRec, index);
3160b02be92SPatrick Venture     }
3170b02be92SPatrick Venture     else
3180b02be92SPatrick Venture     {
31998a23840SMatthew Barth         // Scroll through each bit position .  Determine
32098a23840SMatthew Barth         // if any bit is either asserted or Deasserted.
3214491a46fSPatrick Venture         for (int i = 0; i < 8; i++)
3220b02be92SPatrick Venture         {
32398a23840SMatthew Barth             if ((ISBITSET(pRec->assert_state7_0, i)) &&
32498a23840SMatthew Barth                 (shouldReport(stype, i, &index)))
32598a23840SMatthew Barth             {
32698a23840SMatthew Barth                 reportSensorEventAssert(pRec, index);
32798a23840SMatthew Barth             }
32898a23840SMatthew Barth             if ((ISBITSET(pRec->assert_state14_8, i)) &&
32998a23840SMatthew Barth                 (shouldReport(stype, i + 8, &index)))
33098a23840SMatthew Barth             {
33198a23840SMatthew Barth                 reportSensorEventAssert(pRec, index);
33298a23840SMatthew Barth             }
33398a23840SMatthew Barth             if ((ISBITSET(pRec->deassert_state7_0, i)) &&
33498a23840SMatthew Barth                 (shouldReport(stype, i, &index)))
33598a23840SMatthew Barth             {
33698a23840SMatthew Barth                 reportSensorEventDeassert(pRec, index);
33798a23840SMatthew Barth             }
33898a23840SMatthew Barth             if ((ISBITSET(pRec->deassert_state14_8, i)) &&
33998a23840SMatthew Barth                 (shouldReport(stype, i + 8, &index)))
34098a23840SMatthew Barth             {
34198a23840SMatthew Barth                 reportSensorEventDeassert(pRec, index);
34298a23840SMatthew Barth             }
34398a23840SMatthew Barth         }
34498a23840SMatthew Barth     }
34598a23840SMatthew Barth 
34698a23840SMatthew Barth     return 0;
34798a23840SMatthew Barth }
348