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" |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 18 | #include "handler.hpp" |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 19 | |
| 20 | #include <cstdint> |
| 21 | #include <cstring> |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 22 | #include <ipmid/api-types.hpp> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 23 | #include <string> |
Patrick Venture | 72e1a88 | 2019-03-16 11:43:08 -0700 | [diff] [blame] | 24 | #include <tuple> |
| 25 | #include <vector> |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 26 | |
| 27 | namespace google |
| 28 | { |
| 29 | namespace ipmi |
| 30 | { |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 31 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 32 | #ifndef MAX_IPMI_BUFFER |
| 33 | #define MAX_IPMI_BUFFER 64 |
| 34 | #endif |
| 35 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 36 | struct PcieSlotI2cBusMappingRequest |
| 37 | { |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 38 | uint8_t entry; |
| 39 | } __attribute__((packed)); |
| 40 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 41 | Resp pcieSlotCount(const std::vector<std::uint8_t>&, HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 42 | { |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 43 | // If there are already entries in the vector, clear them. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 44 | handler->buildI2cPcieMapping(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 45 | |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 46 | // Fill the pcie slot count as the number of entries in the vector. |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 47 | std::uint8_t value = handler->getI2cPcieMappingSize(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 48 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 49 | return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount, |
| 50 | std::vector<std::uint8_t>{value}); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 53 | Resp pcieSlotI2cBusMapping(const std::vector<std::uint8_t>& data, |
| 54 | HandlerInterface* handler) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 55 | { |
| 56 | struct PcieSlotI2cBusMappingRequest request; |
| 57 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 58 | if (data.size() < sizeof(request)) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 59 | { |
| 60 | std::fprintf(stderr, "Invalid command length: %u\n", |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 61 | static_cast<uint32_t>(data.size())); |
| 62 | return ::ipmi::responseReqDataLenInvalid(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // If there are no entries in the vector return error. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 66 | size_t mapSize = handler->getI2cPcieMappingSize(); |
| 67 | if (mapSize == 0) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 68 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 69 | return ::ipmi::responseInvalidReservationId(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 72 | std::memcpy(&request, data.data(), sizeof(request)); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 73 | |
| 74 | // The valid entries range from 0 to N - 1, N being the total number of |
| 75 | // entries in the vector. |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 76 | if (request.entry >= mapSize) |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 77 | { |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 78 | return ::ipmi::responseParmOutOfRange(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // 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] | 82 | auto i2cEntry = handler->getI2cEntry(request.entry); |
| 83 | uint32_t i2c_bus_number = std::get<0>(i2cEntry); |
| 84 | std::string pcie_slot_name = std::get<1>(i2cEntry); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 85 | |
| 86 | int length = |
| 87 | sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length(); |
| 88 | |
| 89 | // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER |
| 90 | // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES. |
| 91 | if (length > MAX_IPMI_BUFFER) |
| 92 | { |
| 93 | std::fprintf(stderr, "Response would overflow response buffer\n"); |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 94 | return ::ipmi::responseInvalidCommand(); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 97 | std::vector<std::uint8_t> reply; |
| 98 | reply.reserve(pcie_slot_name.length() + |
| 99 | sizeof(struct PcieSlotI2cBusMappingReply)); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 100 | // Copy the i2c bus number and the pcie slot name to the reply struct. |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 101 | reply.emplace_back(i2c_bus_number); /* i2c_bus_number */ |
| 102 | reply.emplace_back(pcie_slot_name.length()); /* pcie_slot_name length */ |
| 103 | reply.insert(reply.end(), pcie_slot_name.begin(), |
| 104 | pcie_slot_name.end()); /* pcie_slot_name */ |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 105 | |
Willy Tu | ff3cd8e | 2021-09-14 22:49:55 -0700 | [diff] [blame^] | 106 | return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping, |
| 107 | reply); |
Jaghathiswari Rankappagounder Natarajan | 2d4836d | 2018-11-29 14:16:39 -0800 | [diff] [blame] | 108 | } |
| 109 | } // namespace ipmi |
| 110 | } // namespace google |