blob: 0a5973833070ffa8a434b86a6fb5f0dc0b4e38b6 [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
Patrick Williams444b5ea2023-05-19 13:56:42 -050020#include <ipmid/api-types.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000021#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050022
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080023#include <cstdint>
24#include <cstring>
Willy Tub4e37042021-10-12 17:12:30 -070025#include <span>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080026#include <string>
Patrick Venture72e1a882019-03-16 11:43:08 -070027#include <tuple>
28#include <vector>
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080029
30namespace google
31{
32namespace ipmi
33{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080034
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080035#ifndef MAX_IPMI_BUFFER
36#define MAX_IPMI_BUFFER 64
37#endif
38
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080039struct PcieSlotI2cBusMappingRequest
40{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080041 uint8_t entry;
42} __attribute__((packed));
43
Willy Tub4e37042021-10-12 17:12:30 -070044Resp pcieSlotCount(std::span<const uint8_t>, HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080045{
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080046 // If there are already entries in the vector, clear them.
Patrick Venture49f23ad2019-03-16 11:59:55 -070047 handler->buildI2cPcieMapping();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080048
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080049 // Fill the pcie slot count as the number of entries in the vector.
Willy Tuff3cd8e2021-09-14 22:49:55 -070050 std::uint8_t value = handler->getI2cPcieMappingSize();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080051
Willy Tuff3cd8e2021-09-14 22:49:55 -070052 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotCount,
53 std::vector<std::uint8_t>{value});
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080054}
55
Willy Tub4e37042021-10-12 17:12:30 -070056Resp pcieSlotI2cBusMapping(std::span<const uint8_t> data,
Willy Tuff3cd8e2021-09-14 22:49:55 -070057 HandlerInterface* handler)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080058{
59 struct PcieSlotI2cBusMappingRequest request;
60
Willy Tuff3cd8e2021-09-14 22:49:55 -070061 if (data.size() < sizeof(request))
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080062 {
Michael Shen8d618532023-10-25 09:14:07 +000063 stdplus::print(stderr, "Invalid command length: {}\n", data.size());
Willy Tuff3cd8e2021-09-14 22:49:55 -070064 return ::ipmi::responseReqDataLenInvalid();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080065 }
66
67 // If there are no entries in the vector return error.
Patrick Venture49f23ad2019-03-16 11:59:55 -070068 size_t mapSize = handler->getI2cPcieMappingSize();
69 if (mapSize == 0)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080070 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070071 return ::ipmi::responseInvalidReservationId();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080072 }
73
Willy Tuff3cd8e2021-09-14 22:49:55 -070074 std::memcpy(&request, data.data(), sizeof(request));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080075
76 // The valid entries range from 0 to N - 1, N being the total number of
77 // entries in the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070078 if (request.entry >= mapSize)
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080079 {
Willy Tuff3cd8e2021-09-14 22:49:55 -070080 return ::ipmi::responseParmOutOfRange();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080081 }
82
83 // Get the i2c bus number and the pcie slot name from the vector.
Patrick Venture49f23ad2019-03-16 11:59:55 -070084 auto i2cEntry = handler->getI2cEntry(request.entry);
85 uint32_t i2c_bus_number = std::get<0>(i2cEntry);
86 std::string pcie_slot_name = std::get<1>(i2cEntry);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080087
Patrick Williams444b5ea2023-05-19 13:56:42 -050088 int length = sizeof(struct PcieSlotI2cBusMappingReply) +
89 pcie_slot_name.length();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080090
91 // TODO (jaghu) : Add a way to dynamically receive the MAX_IPMI_BUFFER
92 // value and change error to IPMI_CC_REQUESTED_TOO_MANY_BYTES.
93 if (length > MAX_IPMI_BUFFER)
94 {
Michael Shen8d618532023-10-25 09:14:07 +000095 stdplus::print(stderr, "Response would overflow response buffer\n");
Willy Tuff3cd8e2021-09-14 22:49:55 -070096 return ::ipmi::responseInvalidCommand();
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -080097 }
98
Willy Tuff3cd8e2021-09-14 22:49:55 -070099 std::vector<std::uint8_t> reply;
Patrick Williams8c0094e2024-08-16 15:22:37 -0400100 reply.reserve(
101 pcie_slot_name.length() + sizeof(struct PcieSlotI2cBusMappingReply));
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800102 // Copy the i2c bus number and the pcie slot name to the reply struct.
Willy Tuff3cd8e2021-09-14 22:49:55 -0700103 reply.emplace_back(i2c_bus_number); /* i2c_bus_number */
104 reply.emplace_back(pcie_slot_name.length()); /* pcie_slot_name length */
105 reply.insert(reply.end(), pcie_slot_name.begin(),
Willy Tu1209ccc2023-05-19 00:49:51 -0700106 pcie_slot_name.end()); /* pcie_slot_name */
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800107
Willy Tuff3cd8e2021-09-14 22:49:55 -0700108 return ::ipmi::responseSuccess(SysOEMCommands::SysPcieSlotI2cBusMapping,
109 reply);
Jaghathiswari Rankappagounder Natarajan2d4836d2018-11-29 14:16:39 -0800110}
111} // namespace ipmi
112} // namespace google