blob: 4c6fed12a05a9b8137ebd966223c6cad0bb9c4e9 [file] [log] [blame]
Willy Tua2056e92021-10-10 13:36:16 -07001// 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.
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080014
15#include "pcie_i2c.hpp"
16
Patrick Venture0e9aae52020-08-13 13:07:09 -070017#include "commands.hpp"
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080018
19#include <cstdint>
20#include <cstring>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080021#include <string>
Patrick Venture72e1a882019-03-16 11:43:08 -070022#include <tuple>
23#include <vector>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080024
25namespace google
26{
27namespace ipmi
28{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080029
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080030#ifndef MAX_IPMI_BUFFER
31#define MAX_IPMI_BUFFER 64
32#endif
33
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080034struct PcieSlotCountRequest
35{
36 uint8_t subcommand;
37} __attribute__((packed));
38
39struct PcieSlotCountReply
40{
41 uint8_t subcommand;
42 uint8_t value;
43} __attribute__((packed));
44
45struct PcieSlotI2cBusMappingRequest
46{
47 uint8_t subcommand;
48 uint8_t entry;
49} __attribute__((packed));
50
51struct PcieSlotI2cBusMappingReply
52{
53 uint8_t subcommand;
54 uint8_t i2c_bus_number;
55 uint8_t pcie_slot_name_len;
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080056} __attribute__((packed));
57
William A. Kennington III5d789732021-06-24 03:39:31 -070058ipmi_ret_t pcieSlotCount(const uint8_t*, uint8_t* replyBuf, size_t* dataLen,
59 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080060{
61 if ((*dataLen) < sizeof(struct PcieSlotCountRequest))
62 {
63 std::fprintf(stderr, "Invalid command length: %u\n",
64 static_cast<uint32_t>(*dataLen));
65 return IPMI_CC_REQ_DATA_LEN_INVALID;
66 }
67
68 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070069 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080070
71 struct PcieSlotCountReply reply;
72 reply.subcommand = SysPcieSlotCount;
73 // Fill the pcie slot count as the number of entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070074 reply.value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080075
76 std::memcpy(&replyBuf[0], &reply, sizeof(reply));
77
78 // Return the subcommand and the result.
79 (*dataLen) = sizeof(reply);
80
81 return IPMI_CC_OK;
82}
83
Patrick Venture45fad1b2019-03-18 16:52:14 -070084ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venture49f23ad2019-03-16 11:59:55 -070085 size_t* dataLen, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080086{
87 struct PcieSlotI2cBusMappingRequest request;
88
89 if ((*dataLen) < sizeof(request))
90 {
91 std::fprintf(stderr, "Invalid command length: %u\n",
92 static_cast<uint32_t>(*dataLen));
93 return IPMI_CC_REQ_DATA_LEN_INVALID;
94 }
95
96 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -070097 size_t mapSize = handler->getI2cPcieMappingSize();
98 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080099 {
100 return IPMI_CC_INVALID_RESERVATION_ID;
101 }
102
103 std::memcpy(&request, &reqBuf[0], sizeof(request));
104
105 // The valid entries range from 0 to N - 1, N being the total number of
106 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700107 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800108 {
109 return IPMI_CC_PARM_OUT_OF_RANGE;
110 }
111
112 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700113 auto i2cEntry = handler->getI2cEntry(request.entry);
114 uint32_t i2c_bus_number = std::get<0>(i2cEntry);
115 std::string pcie_slot_name = std::get<1>(i2cEntry);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800116
117 int length =
118 sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length();
119
120 // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER
121 // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES.
122 if (length > MAX_IPMI_BUFFER)
123 {
124 std::fprintf(stderr, "Response would overflow response buffer\n");
125 return IPMI_CC_INVALID;
126 }
127
128 auto reply =
129 reinterpret_cast<struct PcieSlotI2cBusMappingReply*>(&replyBuf[0]);
130 reply->subcommand = SysPcieSlotI2cBusMapping;
131 // Copy the i2c bus number and the pcie slot name to the reply struct.
132 reply->i2c_bus_number = i2c_bus_number;
133 reply->pcie_slot_name_len = pcie_slot_name.length();
William A. Kennington III5d789732021-06-24 03:39:31 -0700134 std::memcpy(reply + 1, pcie_slot_name.c_str(), pcie_slot_name.length());
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800135
136 // Return the subcommand and the result.
137 (*dataLen) = length;
138 return IPMI_CC_OK;
139}
140} // namespace ipmi
141} // namespace google