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 | |
| 80 | // Amester packs data in 4 bytes |
| 81 | ssize_t size = command.size() * sizeof(int32_t); |
| 82 | auto rc = write((fd)(), command.data(), size); |
| 83 | if (rc < 0 || (rc != size)) |
| 84 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame^] | 85 | // This would log and terminate since its not handled. |
| 86 | elog<WriteFailure>( |
| 87 | phosphor::logging::org::open_power::OCC::PassThrough:: |
| 88 | WriteFailure::CALLOUT_ERRNO(errno), |
| 89 | phosphor::logging::org::open_power::OCC::PassThrough:: |
| 90 | WriteFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Now read the response. This would be the content of occ-sram |
| 94 | while(1) |
| 95 | { |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 96 | int32_t data {}; |
| 97 | auto len = read((fd)(), &data, sizeof(data)); |
| 98 | if (len > 0) |
| 99 | { |
| 100 | response.emplace_back(data); |
| 101 | } |
| 102 | else if (len < 0 && errno == EAGAIN) |
| 103 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame^] | 104 | // We may have data coming still. |
| 105 | // This driver does not need a sleep for a retry. |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 106 | continue; |
| 107 | } |
| 108 | else if (len == 0) |
| 109 | { |
| 110 | // We have read all that we can. |
| 111 | break; |
| 112 | } |
| 113 | else |
| 114 | { |
Vishwanatha Subbanna | 9bb065b | 2017-04-18 14:25:26 +0530 | [diff] [blame^] | 115 | // This would log and terminate since its not handled. |
| 116 | elog<ReadFailure>( |
| 117 | phosphor::logging::org::open_power::OCC::PassThrough:: |
| 118 | ReadFailure::CALLOUT_ERRNO(errno), |
| 119 | phosphor::logging::org::open_power::OCC::PassThrough:: |
| 120 | ReadFailure::CALLOUT_DEVICE_PATH(devicePath.c_str())); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | return response; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 127 | } // namespace pass_through |
| 128 | } // namespace occ |
| 129 | } // namespace open_power |