blob: 46088a5afbb5c1dc8d808925b665c3487861ba8d [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>
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 PcieSlotI2cBusMappingRequest
37{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080038 uint8_t entry;
39} __attribute__((packed));
40
Willy Tuff3cd8e2021-09-14 22:49:55 -070041Resp pcieSlotCount(const std::vector<std::uint8_t>&, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080042{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080043 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070044 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080045
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080046 // Fill the pcie slot count as the number of entries in the vector.
Willy Tuff3cd8e2021-09-14 22:49:55 -070047 std::uint8_t value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080048
Willy Tuff3cd8e2021-09-14 22:49:55 -070049 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount,
50 std::vector<std::uint8_t>{value});
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080051}
52
Willy Tuff3cd8e2021-09-14 22:49:55 -070053Resp pcieSlotI2cBusMapping(const std::vector<std::uint8_t>& data,
54 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080055{
56 struct PcieSlotI2cBusMappingRequest request;
57
Willy Tuff3cd8e2021-09-14 22:49:55 -070058 if (data.size() < sizeof(request))
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080059 {
60 std::fprintf(stderr, "Invalid command length: %u\n",
Willy Tuff3cd8e2021-09-14 22:49:55 -070061 static_cast<uint32_t>(data.size()));
62 return ::ipmi::responseReqDataLenInvalid();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080063 }
64
65 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -070066 size_t mapSize = handler->getI2cPcieMappingSize();
67 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080068 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070069 return ::ipmi::responseInvalidReservationId();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080070 }
71
Willy Tuff3cd8e2021-09-14 22:49:55 -070072 std::memcpy(&request, data.data(), sizeof(request));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080073
74 // The valid entries range from 0 to N - 1, N being the total number of
75 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070076 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080077 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070078 return ::ipmi::responseParmOutOfRange();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080079 }
80
81 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070082 auto i2cEntry = handler->getI2cEntry(request.entry);
83 uint32_t i2c_bus_number = std::get<0>(i2cEntry);
84 std::string pcie_slot_name = std::get<1>(i2cEntry);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080085
86 int length =
87 sizeof(struct PcieSlotI2cBusMappingReply) + pcie_slot_name.length();
88
89 // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER
90 // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES.
91 if (length > MAX_IPMI_BUFFER)
92 {
93 std::fprintf(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070094 return ::ipmi::responseInvalidCommand();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080095 }
96
Willy Tuff3cd8e2021-09-14 22:49:55 -070097 std::vector<std::uint8_t> reply;
98 reply.reserve(pcie_slot_name.length() +
99 sizeof(struct PcieSlotI2cBusMappingReply));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800100 // Copy the i2c bus number and the pcie slot name to the reply struct.
Willy Tuff3cd8e2021-09-14 22:49:55 -0700101 reply.emplace_back(i2c_bus_number); /* i2c_bus_number */
102 reply.emplace_back(pcie_slot_name.length()); /* pcie_slot_name length */
103 reply.insert(reply.end(), pcie_slot_name.begin(),
104 pcie_slot_name.end()); /* pcie_slot_name */
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800105
Willy Tuff3cd8e2021-09-14 22:49:55 -0700106 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping,
107 reply);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800108}
109} // namespace ipmi
110} // namespace google