Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame^] | 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. |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 14 | |
| 15 | #include "pcie_i2c.hpp" |
| 16 | |
Patrick Venture | 0e9aae5 | 2020-08-13 13:07:09 -0700 | [diff] [blame] | 17 | #include "commands.hpp" |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 18 | |
| 19 | #include <cstdint> |
| 20 | #include <cstring> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 21 | #include <string> |
Patrick Venture | 72e1a88 | 2019-03-16 11:43:08 -0700 | [diff] [blame] | 22 | #include <tuple> |
| 23 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 24 | |
| 25 | namespace google |
| 26 | { |
| 27 | namespace ipmi |
| 28 | { |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 29 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 30 | #ifndef MAX_IPMI_BUFFER |
| 31 | #define MAX_IPMI_BUFFER 64 |
| 32 | #endif |
| 33 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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; |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 56 | } __attribute__((packed)); |
| 57 | |
William A. Kennington III | 5d78973 | 2021-06-24 03:39:31 -0700 | [diff] [blame] | 58 | ipmi_ret_t pcieSlotCount(const uint8_t*, uint8_t* replyBuf, size_t* dataLen, |
| 59 | HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 69 | handler->buildI2cPcieMapping(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 70 | |
| 71 | struct PcieSlotCountReply reply; |
| 72 | reply.subcommand = SysPcieSlotCount; |
| 73 | // 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] | 74 | reply.value = handler->getI2cPcieMappingSize(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 45fad1b | 2019-03-18 16:52:14 -0700 | [diff] [blame] | 84 | ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf, |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 85 | size_t* dataLen, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 97 | size_t mapSize = handler->getI2cPcieMappingSize(); |
| 98 | if (mapSize == 0) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 107 | if (request.entry >= mapSize) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 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); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 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(); |
William A. Kennington III | 5d78973 | 2021-06-24 03:39:31 -0700 | [diff] [blame] | 134 | std::memcpy(reply + 1, pcie_slot_name.c_str(), pcie_slot_name.length()); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 135 | |
| 136 | // Return the subcommand and the result. |
| 137 | (*dataLen) = length; |
| 138 | return IPMI_CC_OK; |
| 139 | } |
| 140 | } // namespace ipmi |
| 141 | } // namespace google |