Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 1 | // Copyright 2022 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 "config.h" |
| 16 | |
| 17 | #include "pcie_bifurcation.hpp" |
| 18 | |
| 19 | #include "commands.hpp" |
| 20 | #include "errors.hpp" |
| 21 | #include "handler.hpp" |
| 22 | |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 23 | #include <ipmid/api-types.hpp> |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 24 | #include <stdplus/print.hpp> |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 25 | |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 26 | #include <span> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace google |
| 30 | { |
| 31 | namespace ipmi |
| 32 | { |
| 33 | |
| 34 | namespace |
| 35 | { |
| 36 | #ifndef MAX_IPMI_BUFFER |
| 37 | #define MAX_IPMI_BUFFER 64 |
| 38 | #endif |
| 39 | } // namespace |
| 40 | |
| 41 | struct PcieBifurcationRequest |
| 42 | { |
| 43 | uint8_t pcieIndex; |
| 44 | } __attribute__((packed)); |
| 45 | |
| 46 | Resp pcieBifurcation(std::span<const uint8_t> data, HandlerInterface* handler) |
| 47 | { |
| 48 | if (data.size() < sizeof(struct PcieBifurcationRequest)) |
| 49 | { |
Willy Tu | c5b5523 | 2023-11-01 09:27:40 -0700 | [diff] [blame] | 50 | stdplus::print(stderr, "Invalid command length: {}\n", |
| 51 | static_cast<uint32_t>(data.size())); |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 52 | return ::ipmi::responseReqDataLenInvalid(); |
| 53 | } |
| 54 | |
| 55 | auto bifurcation = handler->pcieBifurcation(/*index=*/data[0]); |
| 56 | |
| 57 | int length = sizeof(struct PcieBifurcationReply) + bifurcation.size(); |
| 58 | |
| 59 | if (length > MAX_IPMI_BUFFER) |
| 60 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 61 | stdplus::print(stderr, "Response would overflow response buffer\n"); |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 62 | return ::ipmi::responseInvalidCommand(); |
| 63 | } |
| 64 | |
| 65 | std::vector<std::uint8_t> reply; |
| 66 | reply.reserve(bifurcation.size() + sizeof(struct PcieBifurcationReply)); |
| 67 | reply.emplace_back(bifurcation.size()); /* bifurcationLength */ |
| 68 | reply.insert(reply.end(), bifurcation.begin(), |
Willy Tu | 1209ccc | 2023-05-19 00:49:51 -0700 | [diff] [blame] | 69 | bifurcation.end()); /* bifurcation */ |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 70 | |
| 71 | return ::ipmi::responseSuccess(SysOEMCommands::SysPCIeSlotBifurcation, |
| 72 | reply); |
| 73 | } |
| 74 | } // namespace ipmi |
| 75 | } // namespace google |