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