Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 1 | #include <memory> |
Deepak Kodihalli | 02ba9ec | 2017-03-18 02:57:43 -0500 | [diff] [blame] | 2 | #include <algorithm> |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 3 | #include <fcntl.h> |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 4 | #include <errno.h> |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 5 | #include <string> |
| 6 | #include <unistd.h> |
Deepak Kodihalli | 02ba9ec | 2017-03-18 02:57:43 -0500 | [diff] [blame] | 7 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/elog.hpp> |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 9 | #include <org/open_power/OCC/Device/error.hpp> |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 10 | #include "occ_pass_through.hpp" |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 11 | #include "elog-errors.hpp" |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 12 | #include "config.h" |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 13 | namespace open_power |
| 14 | { |
| 15 | namespace occ |
| 16 | { |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 17 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 18 | PassThrough::PassThrough( |
| 19 | sdbusplus::bus::bus& bus, |
| 20 | const char* path) : |
| 21 | Iface(bus, path), |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 22 | path(path), |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 23 | devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)), |
| 24 | activeStatusSignal( |
| 25 | bus, |
| 26 | sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"), |
| 27 | std::bind(std::mem_fn(&PassThrough::activeStatusEvent), |
| 28 | this, std::placeholders::_1)) |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 29 | { |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 30 | // Nothing to do. |
| 31 | } |
| 32 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 33 | void PassThrough::openDevice() |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 34 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 35 | using namespace phosphor::logging; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 36 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 37 | |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 38 | if (!occActive) |
| 39 | { |
| 40 | log<level::INFO>("OCC is inactive; cannot perform pass-through"); |
| 41 | return; |
| 42 | } |
| 43 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 44 | fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 45 | if (fd < 0) |
| 46 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 47 | // This would log and terminate since its not handled. |
| 48 | elog<OpenFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 49 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 50 | OpenFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 51 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 52 | OpenFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 53 | } |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
| 57 | void PassThrough::closeDevice() |
| 58 | { |
| 59 | if (fd >= 0) |
| 60 | { |
| 61 | close(fd); |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 62 | fd = -1; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 63 | } |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) |
| 67 | { |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 68 | using namespace phosphor::logging; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 69 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 70 | |
| 71 | std::vector<int32_t> response {}; |
| 72 | |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 73 | openDevice(); |
| 74 | |
| 75 | if (fd < 0) |
| 76 | { |
| 77 | // OCC is inactive; empty response |
| 78 | return response; |
| 79 | } |
| 80 | |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 81 | // OCC only understands [bytes] so need array of bytes. Doing this |
| 82 | // because rest-server currently treats all int* as 32 bit integer. |
| 83 | std::vector<uint8_t> cmdInBytes; |
| 84 | cmdInBytes.resize(command.size()); |
| 85 | |
| 86 | // Populate uint8_t version of vector. |
| 87 | std::transform(command.begin(), command.end(), cmdInBytes.begin(), |
| 88 | [](decltype(cmdInBytes)::value_type x){return x;}); |
| 89 | |
| 90 | ssize_t size = cmdInBytes.size() * sizeof(decltype(cmdInBytes)::value_type); |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 91 | auto rc = write(fd, cmdInBytes.data(), size); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 92 | if (rc < 0 || (rc != size)) |
| 93 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 94 | // This would log and terminate since its not handled. |
| 95 | elog<WriteFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 96 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 97 | WriteFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 98 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 99 | WriteFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Now read the response. This would be the content of occ-sram |
| 103 | while(1) |
| 104 | { |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 105 | uint8_t data {}; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 106 | auto len = read(fd, &data, sizeof(data)); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 107 | if (len > 0) |
| 108 | { |
| 109 | response.emplace_back(data); |
| 110 | } |
| 111 | else if (len < 0 && errno == EAGAIN) |
| 112 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 113 | // We may have data coming still. |
| 114 | // This driver does not need a sleep for a retry. |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 115 | continue; |
| 116 | } |
| 117 | else if (len == 0) |
| 118 | { |
| 119 | // We have read all that we can. |
| 120 | break; |
| 121 | } |
| 122 | else |
| 123 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 124 | // This would log and terminate since its not handled. |
| 125 | elog<ReadFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 126 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 127 | ReadFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 128 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 129 | ReadFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 133 | closeDevice(); |
| 134 | |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 135 | return response; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 138 | // Called at OCC Status change signal |
| 139 | void PassThrough::activeStatusEvent(sdbusplus::message::message& msg) |
| 140 | { |
| 141 | std::string statusInterface; |
| 142 | std::map<std::string, sdbusplus::message::variant<bool>> msgData; |
| 143 | msg.read(statusInterface, msgData); |
| 144 | |
| 145 | auto propertyMap = msgData.find("OccActive"); |
| 146 | if (propertyMap != msgData.end()) |
| 147 | { |
| 148 | // Extract the OccActive property |
| 149 | if (sdbusplus::message::variant_ns::get<bool>(propertyMap->second)) |
| 150 | { |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 151 | occActive = true; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | { |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 155 | occActive = false; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 156 | this->closeDevice(); |
| 157 | } |
| 158 | } |
| 159 | return; |
| 160 | } |
| 161 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 162 | } // namespace occ |
| 163 | } // namespace open_power |