xref: /openbmc/google-ipmi-sys/pcie_bifurcation.cpp (revision 8d6185322d2631b9ecceeb0ceb95c03d1f98f27f)
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 
23 #include <fmt/format.h>
24 
25 #include <ipmid/api-types.hpp>
26 #include <stdplus/print.hpp>
27 
28 #include <span>
29 #include <vector>
30 
31 namespace google
32 {
33 namespace ipmi
34 {
35 
36 namespace
37 {
38 #ifndef MAX_IPMI_BUFFER
39 #define MAX_IPMI_BUFFER 64
40 #endif
41 } // namespace
42 
43 struct PcieBifurcationRequest
44 {
45     uint8_t pcieIndex;
46 } __attribute__((packed));
47 
48 Resp 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     {
63         stdplus::print(stderr, "Response would overflow response buffer\n");
64         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(),
71                  bifurcation.end());        /* bifurcation */
72 
73     return ::ipmi::responseSuccess(SysOEMCommands::SysPCIeSlotBifurcation,
74                                    reply);
75 }
76 } // namespace ipmi
77 } // namespace google
78