blob: 2ddfd06ab8558f65cfddb35564fcf09232edb9cf [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
19#include "main.hpp"
Patrick Venture8d5c9ce2019-03-16 11:20:12 -070020#include "util.hpp"
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080021
22#include <cstdint>
23#include <cstring>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080024#include <string>
Patrick Venture72e1a882019-03-16 11:43:08 -070025#include <tuple>
26#include <vector>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080027
28namespace google
29{
30namespace ipmi
31{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080032
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080033#ifndef MAX_IPMI_BUFFER
34#define MAX_IPMI_BUFFER 64
35#endif
36
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080037struct PcieSlotCountRequest
38{
39 uint8_t subcommand;
40} __attribute__((packed));
41
42struct PcieSlotCountReply
43{
44 uint8_t subcommand;
45 uint8_t value;
46} __attribute__((packed));
47
48struct PcieSlotI2cBusMappingRequest
49{
50 uint8_t subcommand;
51 uint8_t entry;
52} __attribute__((packed));
53
54struct PcieSlotI2cBusMappingReply
55{
56 uint8_t subcommand;
57 uint8_t i2c_bus_number;
58 uint8_t pcie_slot_name_len;
59 uint8_t pcie_slot_name[0];
60} __attribute__((packed));
61
Patrick Venture45fad1b2019-03-18 16:52:14 -070062ipmi_ret_t pcieSlotCount(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venture49f23ad2019-03-16 11:59:55 -070063 size_t* dataLen, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080064{
65 if ((*dataLen) < sizeof(struct PcieSlotCountRequest))
66 {
67 std::fprintf(stderr, "Invalid command length: %u\n",
68 static_cast<uint32_t>(*dataLen));
69 return IPMI_CC_REQ_DATA_LEN_INVALID;
70 }
71
72 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070073 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080074
75 struct PcieSlotCountReply reply;
76 reply.subcommand = SysPcieSlotCount;
77 // Fill the pcie slot count as the number of entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070078 reply.value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080079
80 std::memcpy(&replyBuf[0], &reply, sizeof(reply));
81
82 // Return the subcommand and the result.
83 (*dataLen) = sizeof(reply);
84
85 return IPMI_CC_OK;
86}
87
Patrick Venture45fad1b2019-03-18 16:52:14 -070088ipmi_ret_t pcieSlotI2cBusMapping(const uint8_t* reqBuf, uint8_t* replyBuf,
Patrick Venture49f23ad2019-03-16 11:59:55 -070089 size_t* dataLen, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080090{
91 struct PcieSlotI2cBusMappingRequest request;
92
93 if ((*dataLen) < sizeof(request))
94 {
95 std::fprintf(stderr, "Invalid command length: %u\n",
96 static_cast<uint32_t>(*dataLen));
97 return IPMI_CC_REQ_DATA_LEN_INVALID;
98 }
99
100 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700101 size_t mapSize = handler->getI2cPcieMappingSize();
102 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800103 {
104 return IPMI_CC_INVALID_RESERVATION_ID;
105 }
106
107 std::memcpy(&request, &reqBuf[0], sizeof(request));
108
109 // The valid entries range from 0 to N - 1, N being the total number of
110 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700111 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800112 {
113 return IPMI_CC_PARM_OUT_OF_RANGE;
114 }
115
116 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -0700117 auto i2cEntry = handler->getI2cEntry(request.entry);
118 uint32_t i2c_bus_number = std::get<0>(i2cEntry);
119 std::string pcie_slot_name = std::get<1>(i2cEntry);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800120
121 int length =
122 sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length();
123
124 // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER
125 // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES.
126 if (length > MAX_IPMI_BUFFER)
127 {
128 std::fprintf(stderr, "Response would overflow response buffer\n");
129 return IPMI_CC_INVALID;
130 }
131
132 auto reply =
133 reinterpret_cast<struct PcieSlotI2cBusMappingReply*>(&replyBuf[0]);
134 reply->subcommand = SysPcieSlotI2cBusMapping;
135 // Copy the i2c bus number and the pcie slot name to the reply struct.
136 reply->i2c_bus_number = i2c_bus_number;
137 reply->pcie_slot_name_len = pcie_slot_name.length();
138 std::memcpy(reply->pcie_slot_name, pcie_slot_name.c_str(),
139 pcie_slot_name.length());
140
141 // Return the subcommand and the result.
142 (*dataLen) = length;
143 return IPMI_CC_OK;
144}
145} // namespace ipmi
146} // namespace google