blob: 2ccba89f6ae690b499fa2e7392008488c696bc97 [file] [log] [blame]
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -08001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pcie_i2c.hpp"
18
Patrick Venture0e9aae52020-08-13 13:07:09 -070019#include "commands.hpp"
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080020
21#include <cstdint>
22#include <cstring>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080023#include <string>
Patrick Venture72e1a882019-03-16 11:43:08 -070024#include <tuple>
25#include <vector>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080026
27namespace google
28{
29namespace ipmi
30{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080031
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080032#ifndef MAX_IPMI_BUFFER
33#define MAX_IPMI_BUFFER 64
34#endif
35
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080036struct PcieSlotCountRequest
37{
38 uint8_t subcommand;
39} __attribute__((packed));
40
41struct PcieSlotCountReply
42{
43 uint8_t subcommand;
44 uint8_t value;
45} __attribute__((packed));
46
47struct PcieSlotI2cBusMappingRequest
48{
49 uint8_t subcommand;
50 uint8_t entry;
51} __attribute__((packed));
52
53struct PcieSlotI2cBusMappingReply
54{
55 uint8_t subcommand;
56 uint8_t i2c_bus_number;
57 uint8_t pcie_slot_name_len;
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080058} __attribute__((packed));
59
William A. Kennington III5d789732021-06-24 03:39:31 -070060ipmi_ret_t pcieSlotCount(const uint8_t*, uint8_t* replyBuf, size_t* dataLen,
61 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080062{
63 if ((*dataLen) < sizeof(struct PcieSlotCountRequest))
64 {
65 std::fprintf(stderr, "Invalid command length: %u\n",
66 static_cast<uint32_t>(*dataLen));
67 return IPMI_CC_REQ_DATA_LEN_INVALID;
68 }
69
70 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070071 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080072
73 struct PcieSlotCountReply reply;
74 reply.subcommand = SysPcieSlotCount;
75 // Fill the pcie slot count as the number of entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070076 reply.value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080077
78 std::memcpy(&replyBuf[0], &reply, sizeof(reply));
79
80 // Return the subcommand and the result.
81 (*dataLen) = sizeof(reply);
82
83 return IPMI_CC_OK;
84}
85
Patrick Venture45fad1b2019-03-18 16:52:14 -070086ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venture49f23ad2019-03-16 11:59:55 -070087 size_t* dataLen, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080088{
89 struct PcieSlotI2cBusMappingRequest request;
90
91 if ((*dataLen) < sizeof(request))
92 {
93 std::fprintf(stderr, "Invalid command length: %u\n",
94 static_cast<uint32_t>(*dataLen));
95 return IPMI_CC_REQ_DATA_LEN_INVALID;
96 }
97
98 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -070099 size_t mapSize = handler->getI2cPcieMappingSize();
100 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800101 {
102 return IPMI_CC_INVALID_RESERVATION_ID;
103 }
104
105 std::memcpy(&request, &reqBuf[0], sizeof(request));
106
107 // The valid entries range from 0 to N - 1, N being the total number of
108 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700109 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800110 {
111 return IPMI_CC_PARM_OUT_OF_RANGE;
112 }
113
114 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700115 auto i2cEntry = handler->getI2cEntry(request.entry);
116 uint32_t i2c_bus_number = std::get<0>(i2cEntry);
117 std::string pcie_slot_name = std::get<1>(i2cEntry);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800118
119 int length =
120 sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length();
121
122 // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER
123 // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES.
124 if (length > MAX_IPMI_BUFFER)
125 {
126 std::fprintf(stderr, "Response would overflow response buffer\n");
127 return IPMI_CC_INVALID;
128 }
129
130 auto reply =
131 reinterpret_cast<struct PcieSlotI2cBusMappingReply*>(&replyBuf[0]);
132 reply->subcommand = SysPcieSlotI2cBusMapping;
133 // Copy the i2c bus number and the pcie slot name to the reply struct.
134 reply->i2c_bus_number = i2c_bus_number;
135 reply->pcie_slot_name_len = pcie_slot_name.length();
William A. Kennington III5d789732021-06-24 03:39:31 -0700136 std::memcpy(reply + 1, pcie_slot_name.c_str(), pcie_slot_name.length());
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800137
138 // Return the subcommand and the result.
139 (*dataLen) = length;
140 return IPMI_CC_OK;
141}
142} // namespace ipmi
143} // namespace google