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