blob: 8a8b83964bb7e511fb5052eed79edfa3404182ee [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"
Willy Tuff3cd8e2021-09-14 22:49:55 -070018#include "handler.hpp"
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080019
20#include <cstdint>
21#include <cstring>
Willy Tuff3cd8e2021-09-14 22:49:55 -070022#include <ipmid/api-types.hpp>
Willy Tub4e37042021-10-12 17:12:30 -070023#include <span>
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 PcieSlotI2cBusMappingRequest
38{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080039 uint8_t entry;
40} __attribute__((packed));
41
Willy Tub4e37042021-10-12 17:12:30 -070042Resp pcieSlotCount(std::span<const uint8_t>, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080043{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080044 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070045 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080046
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080047 // Fill the pcie slot count as the number of entries in the vector.
Willy Tuff3cd8e2021-09-14 22:49:55 -070048 std::uint8_t value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080049
Willy Tuff3cd8e2021-09-14 22:49:55 -070050 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount,
51 std::vector<std::uint8_t>{value});
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080052}
53
Willy Tub4e37042021-10-12 17:12:30 -070054Resp pcieSlotI2cBusMapping(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070055 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080056{
57 struct PcieSlotI2cBusMappingRequest request;
58
Willy Tuff3cd8e2021-09-14 22:49:55 -070059 if (data.size() < sizeof(request))
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080060 {
61 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070062 static_cast<uint32_t>(data.size()));
63 return ::ipmi::responseReqDataLenInvalid();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080064 }
65
66 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -070067 size_t mapSize = handler->getI2cPcieMappingSize();
68 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080069 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070070 return ::ipmi::responseInvalidReservationId();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080071 }
72
Willy Tuff3cd8e2021-09-14 22:49:55 -070073 std::memcpy(&request, data.data(), sizeof(request));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080074
75 // The valid entries range from 0 to N - 1, N being the total number of
76 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070077 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080078 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070079 return ::ipmi::responseParmOutOfRange();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080080 }
81
82 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070083 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);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080086
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");
Willy Tuff3cd8e2021-09-14 22:49:55 -070095 return ::ipmi::responseInvalidCommand();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080096 }
97
Willy Tuff3cd8e2021-09-14 22:49:55 -070098 std::vector<std::uint8_t> reply;
99 reply.reserve(pcie_slot_name.length() +
100 sizeof(struct PcieSlotI2cBusMappingReply));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800101 // Copy the i2c bus number and the pcie slot name to the reply struct.
Willy Tuff3cd8e2021-09-14 22:49:55 -0700102 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(),
Willy Tu1209ccc2023-05-19 00:49:51 -0700105 pcie_slot_name.end()); /* pcie_slot_name */
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800106
Willy Tuff3cd8e2021-09-14 22:49:55 -0700107 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping,
108 reply);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800109}
110} // namespace ipmi
111} // namespace google