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> |
Deepak Kodihalli | 02ba9ec | 2017-03-18 02:57:43 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 6 | #include <phosphor-logging/elog.hpp> |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 7 | #include <org/open_power/OCC/Device/error.hpp> |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 8 | #include "occ_pass_through.hpp" |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 9 | #include "elog-errors.hpp" |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 10 | namespace open_power |
| 11 | { |
| 12 | namespace occ |
| 13 | { |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 14 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 15 | PassThrough::PassThrough( |
| 16 | sdbusplus::bus::bus& bus, |
| 17 | const char* path) : |
| 18 | Iface(bus, path), |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 19 | path(path), |
| 20 | fd(openDevice()) |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 21 | { |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 22 | // Nothing to do. |
| 23 | } |
| 24 | |
| 25 | int PassThrough::openDevice() |
| 26 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 28 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 29 | |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 30 | // Device instance number starts from 1. |
Vishwanatha Subbanna | afd21a6 | 2017-04-13 20:17:13 +0530 | [diff] [blame] | 31 | devicePath.append(std::to_string((this->path.back() - '0') + 1)); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 32 | |
| 33 | int fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK); |
| 34 | if (fd < 0) |
| 35 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 36 | // This would log and terminate since its not handled. |
| 37 | elog<OpenFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 38 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 39 | OpenFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 40 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 41 | OpenFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 42 | } |
| 43 | return fd; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) |
| 47 | { |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 48 | using namespace phosphor::logging; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 49 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 50 | |
| 51 | std::vector<int32_t> response {}; |
| 52 | |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 53 | // OCC only understands [bytes] so need array of bytes. Doing this |
| 54 | // because rest-server currently treats all int* as 32 bit integer. |
| 55 | std::vector<uint8_t> cmdInBytes; |
| 56 | cmdInBytes.resize(command.size()); |
| 57 | |
| 58 | // Populate uint8_t version of vector. |
| 59 | std::transform(command.begin(), command.end(), cmdInBytes.begin(), |
| 60 | [](decltype(cmdInBytes)::value_type x){return x;}); |
| 61 | |
| 62 | ssize_t size = cmdInBytes.size() * sizeof(decltype(cmdInBytes)::value_type); |
| 63 | auto rc = write((fd)(), cmdInBytes.data(), size); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 64 | if (rc < 0 || (rc != size)) |
| 65 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 66 | // This would log and terminate since its not handled. |
| 67 | elog<WriteFailure>( |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 68 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 69 | WriteFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 70 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 71 | WriteFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // Now read the response. This would be the content of occ-sram |
| 75 | while(1) |
| 76 | { |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 77 | uint8_t data {}; |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 78 | auto len = read((fd)(), &data, sizeof(data)); |
| 79 | if (len > 0) |
| 80 | { |
| 81 | response.emplace_back(data); |
| 82 | } |
| 83 | else if (len < 0 && errno == EAGAIN) |
| 84 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 85 | // We may have data coming still. |
| 86 | // This driver does not need a sleep for a retry. |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | else if (len == 0) |
| 90 | { |
| 91 | // We have read all that we can. |
| 92 | break; |
| 93 | } |
| 94 | else |
| 95 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 96 | // This would log and terminate since its not handled. |
| 97 | elog<ReadFailure>( |
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 | ReadFailure::CALLOUT_ERRNO(errno), |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame^] | 100 | phosphor::logging::org::open_power::OCC::Device:: |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame] | 101 | ReadFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | return response; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 108 | } // namespace occ |
| 109 | } // namespace open_power |