xref: /openbmc/google-ipmi-sys/pcie_i2c.cpp (revision 8c0094e4)
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.
14 
15 #include "pcie_i2c.hpp"
16 
17 #include "commands.hpp"
18 #include "handler.hpp"
19 
20 #include <ipmid/api-types.hpp>
21 #include <stdplus/print.hpp>
22 
23 #include <cstdint>
24 #include <cstring>
25 #include <span>
26 #include <string>
27 #include <tuple>
28 #include <vector>
29 
30 namespace google
31 {
32 namespace ipmi
33 {
34 
35 #ifndef MAX_IPMI_BUFFER
36 #define MAX_IPMI_BUFFER 64
37 #endif
38 
39 struct PcieSlotI2cBusMappingRequest
40 {
41     uint8_t entry;
42 } __attribute__((packed));
43 
pcieSlotCount(std::span<const uint8_t>,HandlerInterface * handler)44 Resp pcieSlotCount(std::span<const uint8_t>, HandlerInterface* handler)
45 {
46     // If there are already entries in the vector, clear them.
47     handler->buildI2cPcieMapping();
48 
49     // Fill the pcie slot count as the number of entries in the vector.
50     std::uint8_t value = handler->getI2cPcieMappingSize();
51 
52     return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount,
53                                    std::vector<std::uint8_t>{value});
54 }
55 
pcieSlotI2cBusMapping(std::span<const uint8_t> data,HandlerInterface * handler)56 Resp pcieSlotI2cBusMapping(std::span<const uint8_t> data,
57                            HandlerInterface* handler)
58 {
59     struct PcieSlotI2cBusMappingRequest request;
60 
61     if (data.size() < sizeof(request))
62     {
63         stdplus::print(stderr, "Invalid command length: {}\n", data.size());
64         return ::ipmi::responseReqDataLenInvalid();
65     }
66 
67     // If there are no entries in the vector return error.
68     size_t mapSize = handler->getI2cPcieMappingSize();
69     if (mapSize == 0)
70     {
71         return ::ipmi::responseInvalidReservationId();
72     }
73 
74     std::memcpy(&request, data.data(), sizeof(request));
75 
76     // The valid entries range from 0 to N - 1, N being the total number of
77     // entries in the vector.
78     if (request.entry >= mapSize)
79     {
80         return ::ipmi::responseParmOutOfRange();
81     }
82 
83     // Get the i2c bus number and the pcie slot name from the vector.
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);
87 
88     int length = sizeof(struct PcieSlotI2cBusMappingReply) +
89                  pcie_slot_name.length();
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     {
95         stdplus::print(stderr, "Response would overflow response buffer\n");
96         return ::ipmi::responseInvalidCommand();
97     }
98 
99     std::vector<std::uint8_t> reply;
100     reply.reserve(
101         pcie_slot_name.length() + sizeof(struct PcieSlotI2cBusMappingReply));
102     // Copy the i2c bus number and the pcie slot name to the reply struct.
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(),
106                  pcie_slot_name.end());          /* pcie_slot_name */
107 
108     return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping,
109                                    reply);
110 }
111 } // namespace ipmi
112 } // namespace google
113