blob: 4722853f377fc6c5788338d884eda17e4c9b38c0 [file] [log] [blame]
Willy Tu6c71b0f2021-10-10 13:34:41 -07001// 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
23#include <fmt/format.h>
24
25#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000026#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050027
Willy Tu6c71b0f2021-10-10 13:34:41 -070028#include <span>
29#include <vector>
30
31namespace google
32{
33namespace ipmi
34{
35
36namespace
37{
38#ifndef MAX_IPMI_BUFFER
39#define MAX_IPMI_BUFFER 64
40#endif
41} // namespace
42
43struct PcieBifurcationRequest
44{
45 uint8_t pcieIndex;
46} __attribute__((packed));
47
48Resp pcieBifurcation(std::span<const uint8_t> data, HandlerInterface* handler)
49{
50 if (data.size() < sizeof(struct PcieBifurcationRequest))
51 {
52 fmt::print(stderr, "Invalid command length: {}\n",
53 static_cast<uint32_t>(data.size()));
54 return ::ipmi::responseReqDataLenInvalid();
55 }
56
57 auto bifurcation = handler->pcieBifurcation(/*index=*/data[0]);
58
59 int length = sizeof(struct PcieBifurcationReply) + bifurcation.size();
60
61 if (length > MAX_IPMI_BUFFER)
62 {
Michael Shen8d618532023-10-25 09:14:07 +000063 stdplus::print(stderr, "Response would overflow response buffer\n");
Willy Tu6c71b0f2021-10-10 13:34:41 -070064 return ::ipmi::responseInvalidCommand();
65 }
66
67 std::vector<std::uint8_t> reply;
68 reply.reserve(bifurcation.size() + sizeof(struct PcieBifurcationReply));
69 reply.emplace_back(bifurcation.size()); /* bifurcationLength */
70 reply.insert(reply.end(), bifurcation.begin(),
Willy Tu1209ccc2023-05-19 00:49:51 -070071 bifurcation.end()); /* bifurcation */
Willy Tu6c71b0f2021-10-10 13:34:41 -070072
73 return ::ipmi::responseSuccess(SysOEMCommands::SysPCIeSlotBifurcation,
74 reply);
75}
76} // namespace ipmi
77} // namespace google