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