Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "pcie_i2c.hpp" |
| 18 | |
| 19 | #include "main.hpp" |
Patrick Venture | 8d5c9ce | 2019-03-16 11:20:12 -0700 | [diff] [blame] | 20 | #include "util.hpp" |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 21 | |
| 22 | #include <cstdint> |
| 23 | #include <cstring> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 24 | #include <string> |
Patrick Venture | 72e1a88 | 2019-03-16 11:43:08 -0700 | [diff] [blame] | 25 | #include <tuple> |
| 26 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 27 | |
| 28 | namespace google |
| 29 | { |
| 30 | namespace ipmi |
| 31 | { |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 32 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 33 | #ifndef MAX_IPMI_BUFFER |
| 34 | #define MAX_IPMI_BUFFER 64 |
| 35 | #endif |
| 36 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 37 | struct PcieSlotCountRequest |
| 38 | { |
| 39 | uint8_t subcommand; |
| 40 | } __attribute__((packed)); |
| 41 | |
| 42 | struct PcieSlotCountReply |
| 43 | { |
| 44 | uint8_t subcommand; |
| 45 | uint8_t value; |
| 46 | } __attribute__((packed)); |
| 47 | |
| 48 | struct PcieSlotI2cBusMappingRequest |
| 49 | { |
| 50 | uint8_t subcommand; |
| 51 | uint8_t entry; |
| 52 | } __attribute__((packed)); |
| 53 | |
| 54 | struct PcieSlotI2cBusMappingReply |
| 55 | { |
| 56 | uint8_t subcommand; |
| 57 | uint8_t i2c_bus_number; |
| 58 | uint8_t pcie_slot_name_len; |
| 59 | uint8_t pcie_slot_name[0]; |
| 60 | } __attribute__((packed)); |
| 61 | |
| 62 | ipmi_ret_t PcieSlotCount(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 63 | size_t* dataLen, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 64 | { |
| 65 | if ((*dataLen) < sizeof(struct PcieSlotCountRequest)) |
| 66 | { |
| 67 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 68 | static_cast<uint32_t>(*dataLen)); |
| 69 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 70 | } |
| 71 | |
| 72 | // If there are already entries in the vector, clear them. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 73 | handler->buildI2cPcieMapping(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 74 | |
| 75 | struct PcieSlotCountReply reply; |
| 76 | reply.subcommand = SysPcieSlotCount; |
| 77 | // Fill the pcie slot count as the number of entries in the vector. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 78 | reply.value = handler->getI2cPcieMappingSize(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 79 | |
| 80 | std::memcpy(&replyBuf[0], &reply, sizeof(reply)); |
| 81 | |
| 82 | // Return the subcommand and the result. |
| 83 | (*dataLen) = sizeof(reply); |
| 84 | |
| 85 | return IPMI_CC_OK; |
| 86 | } |
| 87 | |
| 88 | ipmi_ret_t PcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 89 | size_t* dataLen, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 90 | { |
| 91 | struct PcieSlotI2cBusMappingRequest request; |
| 92 | |
| 93 | if ((*dataLen) < sizeof(request)) |
| 94 | { |
| 95 | std::fprintf(stderr, "Invalid command length: %u\n", |
| 96 | static_cast<uint32_t>(*dataLen)); |
| 97 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 98 | } |
| 99 | |
| 100 | // If there are no entries in the vector return error. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 101 | size_t mapSize = handler->getI2cPcieMappingSize(); |
| 102 | if (mapSize == 0) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 103 | { |
| 104 | return IPMI_CC_INVALID_RESERVATION_ID; |
| 105 | } |
| 106 | |
| 107 | std::memcpy(&request, &reqBuf[0], sizeof(request)); |
| 108 | |
| 109 | // The valid entries range from 0 to N - 1, N being the total number of |
| 110 | // entries in the vector. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 111 | if (request.entry >= mapSize) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 112 | { |
| 113 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 114 | } |
| 115 | |
| 116 | // Get the i2c bus number and the pcie slot name from the vector. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame^] | 117 | auto i2cEntry = handler->getI2cEntry(request.entry); |
| 118 | uint32_t i2c_bus_number = std::get<0>(i2cEntry); |
| 119 | std::string pcie_slot_name = std::get<1>(i2cEntry); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 120 | |
| 121 | int length = |
| 122 | sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length(); |
| 123 | |
| 124 | // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER |
| 125 | // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES. |
| 126 | if (length > MAX_IPMI_BUFFER) |
| 127 | { |
| 128 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
| 129 | return IPMI_CC_INVALID; |
| 130 | } |
| 131 | |
| 132 | auto reply = |
| 133 | reinterpret_cast<struct PcieSlotI2cBusMappingReply*>(&replyBuf[0]); |
| 134 | reply->subcommand = SysPcieSlotI2cBusMapping; |
| 135 | // Copy the i2c bus number and the pcie slot name to the reply struct. |
| 136 | reply->i2c_bus_number = i2c_bus_number; |
| 137 | reply->pcie_slot_name_len = pcie_slot_name.length(); |
| 138 | std::memcpy(reply->pcie_slot_name, pcie_slot_name.c_str(), |
| 139 | pcie_slot_name.length()); |
| 140 | |
| 141 | // Return the subcommand and the result. |
| 142 | (*dataLen) = length; |
| 143 | return IPMI_CC_OK; |
| 144 | } |
| 145 | } // namespace ipmi |
| 146 | } // namespace google |