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