Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 1 | #include "occ_errors.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 5 | #include <fmt/core.h> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 6 | #include <sys/ioctl.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include <org/open_power/OCC/Device/error.hpp> |
Patrick Williams | d8aab2a | 2023-04-21 11:15:54 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog.hpp> |
| 12 | #include <phosphor-logging/log.hpp> |
| 13 | #include <xyz/openbmc_project/Common/error.hpp> |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 14 | namespace open_power |
| 15 | { |
| 16 | namespace occ |
| 17 | { |
| 18 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 19 | using namespace phosphor::logging; |
| 20 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 21 | using InternalFailure = |
| 22 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 23 | |
| 24 | // Populate the file descriptor on the error file |
| 25 | void Error::openFile() |
| 26 | { |
| 27 | using namespace phosphor::logging; |
| 28 | |
| 29 | fd = open(file.c_str(), O_RDONLY | O_NONBLOCK); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 30 | const int open_errno = errno; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 31 | if (fd < 0) |
| 32 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 33 | log<level::ERR>( |
| 34 | fmt::format("Error::openFile: open failed (errno={})", open_errno) |
| 35 | .c_str()); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 36 | elog<OpenFailure>(phosphor::logging::org::open_power::OCC::Device:: |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 37 | OpenFailure::CALLOUT_ERRNO(open_errno), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 38 | phosphor::logging::org::open_power::OCC::Device:: |
| 39 | OpenFailure::CALLOUT_DEVICE_PATH(file.c_str())); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | // Attaches the FD to event loop and registers the callback handler |
| 44 | void Error::registerCallBack() |
| 45 | { |
| 46 | decltype(eventSource.get()) sourcePtr = nullptr; |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 47 | auto r = sd_event_add_io(event.get(), &sourcePtr, fd, EPOLLPRI | EPOLLERR, |
| 48 | processEvents, this); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 49 | eventSource.reset(sourcePtr); |
| 50 | |
| 51 | if (r < 0) |
| 52 | { |
| 53 | log<level::ERR>("Failed to register callback handler", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 54 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 55 | elog<InternalFailure>(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // Starts to watch for errors |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 60 | void Error::addWatch(bool poll) |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 61 | { |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 62 | if (!watching) |
| 63 | { |
| 64 | // Open the file |
| 65 | openFile(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 66 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 67 | if (poll) |
| 68 | { |
| 69 | // register the callback handler |
| 70 | registerCallBack(); |
| 71 | } |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 72 | |
| 73 | // Set we are watching the error |
| 74 | watching = true; |
| 75 | } |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Stops watching for errors |
| 79 | void Error::removeWatch() |
| 80 | { |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 81 | if (watching) |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 82 | { |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 83 | // Close the file |
| 84 | if (fd >= 0) |
| 85 | { |
| 86 | close(fd); |
| 87 | } |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 88 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 89 | // Reduce the reference count. Since there is only one instances |
| 90 | // of add_io, this will result empty loop |
| 91 | eventSource.reset(); |
| 92 | |
| 93 | // We are no more watching the error |
| 94 | watching = false; |
| 95 | } |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Callback handler when there is an activity on the FD |
George Liu | d0345ae | 2021-09-10 13:13:28 +0800 | [diff] [blame] | 99 | int Error::processEvents(sd_event_source* /*es*/, int /*fd*/, |
| 100 | uint32_t /*revents*/, void* userData) |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 101 | { |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 102 | auto error = static_cast<Error*>(userData); |
| 103 | |
| 104 | error->analyzeEvent(); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | // Reads the error file and analyzes the data |
| 109 | void Error::analyzeEvent() |
| 110 | { |
| 111 | // Get the number of bytes to read |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 112 | int err = 0; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 113 | int len = -1; |
| 114 | auto r = ioctl(fd, FIONREAD, &len); |
| 115 | if (r < 0) |
| 116 | { |
| 117 | elog<ConfigFailure>( |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 118 | phosphor::logging::org::open_power::OCC::Device::ConfigFailure:: |
| 119 | CALLOUT_ERRNO(errno), |
| 120 | phosphor::logging::org::open_power::OCC::Device::ConfigFailure:: |
| 121 | CALLOUT_DEVICE_PATH(file.c_str())); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // A non-zero data indicates an error condition |
| 125 | // Let the caller take appropriate action on this |
| 126 | auto data = readFile(len); |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 127 | if (!data.empty()) |
| 128 | err = std::stoi(data, nullptr, 0); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 129 | if (callBack) |
| 130 | { |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 131 | callBack(err); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 132 | } |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | // Reads so many bytes as passed in |
| 137 | std::string Error::readFile(int len) const |
| 138 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 139 | auto data = std::make_unique<char[]>(len + 1); |
Andrew Geissler | 1111087 | 2018-01-17 11:07:01 -0800 | [diff] [blame] | 140 | auto retries = 3; |
| 141 | auto delay = std::chrono::milliseconds{100}; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 142 | |
Andrew Geissler | 1111087 | 2018-01-17 11:07:01 -0800 | [diff] [blame] | 143 | // OCC / FSI have intermittent issues so retry all reads |
| 144 | while (true) |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 145 | { |
Andrew Geissler | 1111087 | 2018-01-17 11:07:01 -0800 | [diff] [blame] | 146 | // This file get created soon after binding. A value of 0 is |
| 147 | // deemed success and anything else is a Failure |
| 148 | // Since all the sysfs files would have size of 4096, if we read 0 |
| 149 | // bytes -or- value '0', then it just means we are fine |
| 150 | auto r = read(fd, data.get(), len); |
| 151 | if (r < 0) |
| 152 | { |
| 153 | retries--; |
| 154 | if (retries == 0) |
| 155 | { |
| 156 | elog<ReadFailure>( |
| 157 | phosphor::logging::org::open_power::OCC::Device:: |
| 158 | ReadFailure::CALLOUT_ERRNO(errno), |
| 159 | phosphor::logging::org::open_power::OCC::Device:: |
| 160 | ReadFailure::CALLOUT_DEVICE_PATH(file.c_str())); |
| 161 | break; |
| 162 | } |
| 163 | std::this_thread::sleep_for(delay); |
| 164 | continue; |
| 165 | } |
| 166 | break; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 167 | } |
Vishwanatha Subbanna | 2c12913 | 2017-08-29 21:12:01 +0530 | [diff] [blame] | 168 | // Need to seek to START, else the poll returns immediately telling |
| 169 | // there is data to be read |
Andrew Geissler | 1111087 | 2018-01-17 11:07:01 -0800 | [diff] [blame] | 170 | auto r = lseek(fd, 0, SEEK_SET); |
Vishwanatha Subbanna | 2c12913 | 2017-08-29 21:12:01 +0530 | [diff] [blame] | 171 | if (r < 0) |
| 172 | { |
| 173 | log<level::ERR>("Failure seeking error file to START"); |
| 174 | elog<ConfigFailure>( |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 175 | phosphor::logging::org::open_power::OCC::Device::ConfigFailure:: |
| 176 | CALLOUT_ERRNO(errno), |
| 177 | phosphor::logging::org::open_power::OCC::Device::ConfigFailure:: |
| 178 | CALLOUT_DEVICE_PATH(file.c_str())); |
Vishwanatha Subbanna | 2c12913 | 2017-08-29 21:12:01 +0530 | [diff] [blame] | 179 | } |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 180 | return std::string(data.get()); |
| 181 | } |
| 182 | |
| 183 | } // namespace occ |
| 184 | } // namespace open_power |