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