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