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