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 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame^] | 38 | fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 39 | if (fd < 0) |
| 40 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 41 | // This would log and terminate since its not handled. |
| 42 | elog<OpenFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 43 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 44 | OpenFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 45 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 46 | OpenFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 47 | } |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame^] | 48 | return; |
| 49 | } |
| 50 | |
| 51 | void PassThrough::closeDevice() |
| 52 | { |
| 53 | if (fd >= 0) |
| 54 | { |
| 55 | close(fd); |
| 56 | } |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) |
| 60 | { |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 61 | using namespace phosphor::logging; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 62 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 63 | |
| 64 | std::vector<int32_t> response {}; |
| 65 | |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 66 | // OCC only understands [bytes] so need array of bytes. Doing this |
| 67 | // because rest-server currently treats all int* as 32 bit integer. |
| 68 | std::vector<uint8_t> cmdInBytes; |
| 69 | cmdInBytes.resize(command.size()); |
| 70 | |
| 71 | // Populate uint8_t version of vector. |
| 72 | std::transform(command.begin(), command.end(), cmdInBytes.begin(), |
| 73 | [](decltype(cmdInBytes)::value_type x){return x;}); |
| 74 | |
| 75 | ssize_t size = cmdInBytes.size() * sizeof(decltype(cmdInBytes)::value_type); |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame^] | 76 | auto rc = write(fd, cmdInBytes.data(), size); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 77 | if (rc < 0 || (rc != size)) |
| 78 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 79 | // This would log and terminate since its not handled. |
| 80 | elog<WriteFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 81 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 82 | WriteFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 83 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 84 | WriteFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Now read the response. This would be the content of occ-sram |
| 88 | while(1) |
| 89 | { |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 90 | uint8_t data {}; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame^] | 91 | auto len = read(fd, &data, sizeof(data)); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 92 | if (len > 0) |
| 93 | { |
| 94 | response.emplace_back(data); |
| 95 | } |
| 96 | else if (len < 0 && errno == EAGAIN) |
| 97 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 98 | // We may have data coming still. |
| 99 | // This driver does not need a sleep for a retry. |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 100 | continue; |
| 101 | } |
| 102 | else if (len == 0) |
| 103 | { |
| 104 | // We have read all that we can. |
| 105 | break; |
| 106 | } |
| 107 | else |
| 108 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 109 | // This would log and terminate since its not handled. |
| 110 | elog<ReadFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 111 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 112 | ReadFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 113 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 114 | ReadFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | return response; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame^] | 121 | // Called at OCC Status change signal |
| 122 | void PassThrough::activeStatusEvent(sdbusplus::message::message& msg) |
| 123 | { |
| 124 | std::string statusInterface; |
| 125 | std::map<std::string, sdbusplus::message::variant<bool>> msgData; |
| 126 | msg.read(statusInterface, msgData); |
| 127 | |
| 128 | auto propertyMap = msgData.find("OccActive"); |
| 129 | if (propertyMap != msgData.end()) |
| 130 | { |
| 131 | // Extract the OccActive property |
| 132 | if (sdbusplus::message::variant_ns::get<bool>(propertyMap->second)) |
| 133 | { |
| 134 | this->openDevice(); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | this->closeDevice(); |
| 139 | } |
| 140 | } |
| 141 | return; |
| 142 | } |
| 143 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 144 | } // namespace occ |
| 145 | } // namespace open_power |