Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
| 6 | #include "OcpMctpVdm.hpp" |
| 7 | |
| 8 | #include <endian.h> |
| 9 | |
| 10 | #include <cstdint> |
| 11 | #include <cstring> |
| 12 | |
| 13 | namespace ocp |
| 14 | { |
| 15 | namespace accelerator_management |
| 16 | { |
| 17 | |
| 18 | CompletionCode packHeader(uint16_t pciVendorId, const BindingPciVidInfo& hdr, |
| 19 | BindingPciVid& msg) |
| 20 | { |
| 21 | if (hdr.ocp_accelerator_management_msg_type != |
| 22 | static_cast<uint8_t>(MessageType::RESPONSE) && |
| 23 | hdr.ocp_accelerator_management_msg_type != |
| 24 | static_cast<uint8_t>(MessageType::REQUEST)) |
| 25 | { |
| 26 | return CompletionCode::ERR_INVALID_DATA; |
| 27 | } |
| 28 | |
| 29 | if (hdr.instance_id > instanceMax) |
| 30 | { |
| 31 | return CompletionCode::ERR_INVALID_DATA; |
| 32 | } |
| 33 | |
| 34 | msg.datagram = 0; |
| 35 | |
| 36 | msg.request = 0; |
| 37 | if (hdr.ocp_accelerator_management_msg_type == |
| 38 | static_cast<uint8_t>(MessageType::REQUEST)) |
| 39 | { |
| 40 | msg.request = 1; |
| 41 | } |
| 42 | |
| 43 | msg.pci_vendor_id = htobe16(pciVendorId); |
| 44 | msg.reserved = 0; |
| 45 | msg.instance_id = hdr.instance_id; |
| 46 | msg.ocp_type = type; |
| 47 | msg.ocp_version = version; |
| 48 | msg.ocp_accelerator_management_msg_type = hdr.msg_type; |
| 49 | |
| 50 | return CompletionCode::SUCCESS; |
| 51 | } |
| 52 | |
| 53 | CompletionCode encodeReasonCode(uint8_t cc, uint16_t reasonCode, |
| 54 | uint8_t commandCode, Message& msg) |
| 55 | { |
| 56 | CommonNonSuccessResponse response{}; |
| 57 | response.command = commandCode; |
| 58 | response.completion_code = cc; |
| 59 | reasonCode = htole16(reasonCode); |
| 60 | response.reason_code = reasonCode; |
| 61 | |
| 62 | std::memcpy(&msg.data, &response, sizeof(response)); |
| 63 | |
| 64 | return CompletionCode::SUCCESS; |
| 65 | } |
| 66 | |
| 67 | CompletionCode decodeReasonCodeAndCC(const Message& msg, size_t msgLen, |
| 68 | uint8_t& cc, uint16_t& reasonCode) |
| 69 | { |
| 70 | CommonNonSuccessResponse response{}; |
| 71 | std::memcpy(&response, &msg.data, sizeof(response)); |
| 72 | |
| 73 | cc = response.completion_code; |
| 74 | if (cc == static_cast<uint8_t>(CompletionCode::SUCCESS)) |
| 75 | { |
| 76 | return CompletionCode::SUCCESS; |
| 77 | } |
| 78 | |
| 79 | if (msgLen != (sizeof(BindingPciVid) + sizeof(CommonNonSuccessResponse))) |
| 80 | { |
| 81 | return CompletionCode::ERR_INVALID_DATA_LENGTH; |
| 82 | } |
| 83 | |
| 84 | // reason code is expected to be present if CC != SUCCESS |
| 85 | reasonCode = le16toh(response.reason_code); |
| 86 | |
| 87 | return CompletionCode::SUCCESS; |
| 88 | } |
| 89 | } // namespace accelerator_management |
| 90 | } // namespace ocp |