blob: 8c6a0b40b66ac1a8c66ecb5351edee1dd5f5d6ba [file] [log] [blame]
Harshit Agheraa3f24f42025-04-21 20:04:56 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
3 * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
8#include <asm/byteorder.h>
9
10#include <cstddef>
11#include <cstdint>
12
13namespace ocp
14{
15namespace accelerator_management
16{
17
18/** @brief OCP MCTP VDM Message Type
19 *
20 * v1 spec section 3.6.1.2.1
21 */
22constexpr uint8_t messageType = 0x7E;
23
24/**
25 * @defgroup OCP Version
26 *
27 * v1 spec section 3.6.1.2.1
28 * @{
29 */
30constexpr uint8_t type = 8;
31constexpr uint8_t version = 9;
32/** @} */
33
34/**
35 * @defgroup OCP MCTP VDM Instance Id
36 *
37 * v1 spec section 3.6.1.2.1
38 * @{
39 */
40constexpr uint8_t instanceMin = 0;
41constexpr uint8_t instanceIdMask = 0x1F;
42constexpr uint8_t instanceMax = 31;
43/** @} */
44
45/** @brief OCP MCTP VDM completion codes
46 *
47 * v1 spec section 3.6.2
48 */
49enum class CompletionCode : uint8_t
50{
51 SUCCESS = 0x00,
52 ERROR = 0x01,
53 ERR_INVALID_DATA = 0x02,
54 ERR_INVALID_DATA_LENGTH = 0x03,
55 ERR_NOT_READY = 0x04,
56 ERR_UNSUPPORTED_COMMAND_CODE = 0x05,
57 ERR_UNSUPPORTED_MSG_TYPE = 0x06,
58 ERR_BUS_ACCESS = 0x7f,
59 ERR_NULL = 0x80,
60};
61
62/** @brief OCP MCTP VDM reason codes
63 *
64 * v1 spec section 3.6.3
65 */
66enum class ReasonCode : uint16_t
67{
68 REASON_NONE = 0x00,
69};
70
71/** @brief OCP MCTP VDM MessageType
72 *
73 * v1 spec section 3.6.1.2.1
74 */
75enum class MessageType : uint8_t
76{
77 RESPONSE = 0, //!< OCP MCTP VDM response message
78 REQUEST = 2, //!< OCP MCTP VDM request message
79};
80
81/** @struct BindingPciVid
82 *
83 * Structure representing OCP MCTP VDM VDM binding using PCI vendor ID
84 * v1 spec section 3.6.1.2
85 */
86struct BindingPciVid
87{
88 uint16_t pci_vendor_id; //!< PCI defined vendor ID
89
90#if defined(__LITTLE_ENDIAN_BITFIELD)
91 uint8_t instance_id:5; //!< Instance ID
92 uint8_t reserved:1; //!< Reserved
93 uint8_t datagram:1; //!< Datagram bit
94 uint8_t request:1; //!< Request bit
95#elif defined(__BIG_ENDIAN_BITFIELD)
96 uint8_t request:1; //!< Request bit
97 uint8_t datagram:1; //!< Datagram bit
98 uint8_t reserved:1; //!< Reserved
99 uint8_t instance_id:5; //!< Instance ID
100#endif
101
102#if defined(__LITTLE_ENDIAN_BITFIELD)
103 uint8_t ocp_version:4; //!< OCP version
104 uint8_t ocp_type:4; //!< OCP type
105#elif defined(__BIG_ENDIAN_BITFIELD)
106 uint8_t ocp_type:4; //!< OCP type
107 uint8_t ocp_version:4; //!< OCP version
108#endif
109
110 uint8_t ocp_accelerator_management_msg_type; //!< Message Type
111} __attribute__((packed));
112
113/** @struct Message
114 *
115 * Structure representing OCP MCTP VDM message
116 * v1 spec section 3.6.1.2
117 */
118struct Message
119{
120 BindingPciVid hdr; //!< OCP MCTP VDM message header
121 char data; //!< beginning of the payload
122} __attribute__((packed));
123
124/** @struct BindingPciVidInfo
125 *
126 * The information needed to prepare OCP MCTP VDM header and this is passed to
127 * the PackHeader API. v1 spec section 3.6.1.2
128 */
129struct BindingPciVidInfo
130{
131 uint8_t ocp_accelerator_management_msg_type;
132 uint8_t instance_id;
133 uint8_t msg_type;
134};
135
136/** @struct CommonRequest
137 *
138 * Structure representing OCP MCTP VDM request without data (OCP version 1).
139 * v1 spec section 3.6.1.4.1
140 */
141struct CommonRequest
142{
143 uint8_t command;
144 uint8_t data_size;
145} __attribute__((packed));
146
147/** @struct CommonResponse
148 *
149 * Structure representing OCP MCTP VDM response with data
150 * v1 spec section 3.6.1.4.4
151 */
152struct CommonResponse
153{
154 uint8_t command;
155 uint8_t completion_code;
156 uint16_t reserved;
157 uint16_t data_size;
158} __attribute__((packed));
159
160/** @struct CommonNonSuccessResponse
161 *
162 * Structure representing OCP MCTP VDM response with reason code when CC !=
163 * Success v1 spec section 3.6.1.4.5
164 */
165struct CommonNonSuccessResponse
166{
167 uint8_t command;
168 uint8_t completion_code;
169 uint16_t reason_code;
170} __attribute__((packed));
171
172/**
173 * @brief Populate the OCP MCTP VDM message with the OCP MCTP VDM header. OCP
174 * MCTP VDM header OCP Version will be populated with value 1. The caller of
175 * this API allocates buffer for the OCP MCTP VDM header when forming the OCP
176 * MCTP VDM message. The buffer is passed to this API to pack the OCP MCTP VDM
177 * header.
178 *
179 * @param[in] pci_vendor_id - PCI Vendor ID
180 * @param[in] hdr - Pointer to the OCP MCTP VDM header information
181 * @param[out] msg - Reference to OCP MCTP VDM message header
182 *
183 * @return CompletionCode::SUCCESS on success, otherwise appropriate error
184 * code.
185 * @note Caller is responsible for alloc and dealloc of msg
186 * and hdr params
187 */
188CompletionCode packHeader(uint16_t pciVendorId, const BindingPciVidInfo& hdr,
189 BindingPciVid& msg);
190
191/** @brief Encode reason code into an OCP MCTP VDM response message.
192 * This function does not populate or modifies the message header.
193 *
194 * @param[in] cc - Completion Code
195 * @param[in] reason_code - reason code
196 * @param[in] command_code - command code
197 * @param[out] msg - Reference to message
198 * @return CompletionCode
199 */
200CompletionCode encodeReasonCode(uint8_t cc, uint16_t reasonCode,
201 uint8_t commandCode, Message& msg);
202
203/** @brief Decode the reason code
204 *
205 * @param[in] msg - response message
206 * @param[in] msg_len - Length of response message
207 * @param[out] cc - reference to completion code
208 * @param[out] reason_code - reference to reason_code
209 * @return CompletionCode
210 */
211CompletionCode decodeReasonCodeAndCC(const Message& msg, size_t msgLen,
212 uint8_t& cc, uint16_t& reasonCode);
213
214} // namespace accelerator_management
215} // namespace ocp