Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 1 | #ifndef BASE_H |
| 2 | #define BASE_H |
| 3 | |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
| 8 | #include <asm/byteorder.h> |
| 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
| 11 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 12 | #include "pldm_types.h" |
| 13 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 14 | /** @brief PLDM Types |
| 15 | */ |
| 16 | enum pldm_supported_types { |
| 17 | PLDM_BASE = 0x00, |
Sampa Misra | 0db1dfa | 2019-03-19 00:15:31 -0500 | [diff] [blame^] | 18 | PLDM_PLATFORM = 0x02, |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | /** @brief PLDM Commands |
| 22 | */ |
| 23 | enum pldm_supported_commands { |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 24 | PLDM_GET_PLDM_VERSION = 0x3, |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 25 | PLDM_GET_PLDM_TYPES = 0x4, |
| 26 | PLDM_GET_PLDM_COMMANDS = 0x5 |
| 27 | }; |
| 28 | |
| 29 | /** @brief PLDM base codes |
| 30 | */ |
| 31 | enum pldm_completion_codes { |
| 32 | PLDM_SUCCESS = 0x00, |
| 33 | PLDM_ERROR = 0x01, |
| 34 | PLDM_ERROR_INVALID_DATA = 0x02, |
| 35 | PLDM_ERROR_INVALID_LENGTH = 0x03, |
| 36 | PLDM_ERROR_NOT_READY = 0x04, |
| 37 | PLDM_ERROR_UNSUPPORTED_PLDM_CMD = 0x05, |
| 38 | PLDM_ERROR_INVALID_PLDM_TYPE = 0x20 |
| 39 | }; |
| 40 | |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 41 | enum transfer_op_flag { |
| 42 | PLDM_GET_NEXTPART = 0, |
| 43 | PLDM_GET_FIRSTPART = 1, |
| 44 | }; |
| 45 | |
| 46 | enum transfer_resp_flag { |
| 47 | PLDM_START = 0x01, |
| 48 | PLDM_MIDDLE = 0x02, |
| 49 | PLDM_END = 0x04, |
| 50 | PLDM_START_AND_END = 0x05, |
| 51 | }; |
| 52 | |
Tom Joseph | 4125104 | 2019-02-07 16:17:07 +0530 | [diff] [blame] | 53 | /** @enum MessageType |
| 54 | * |
| 55 | * The different message types supported by the PLDM specification. |
| 56 | */ |
| 57 | typedef enum { |
| 58 | PLDM_RESPONSE, //!< PLDM response |
| 59 | PLDM_REQUEST, //!< PLDM request |
| 60 | PLDM_RESERVED, //!< Reserved |
| 61 | PLDM_ASYNC_REQUEST_NOTIFY, //!< Unacknowledged PLDM request messages |
| 62 | } MessageType; |
| 63 | |
| 64 | #define PLDM_INSTANCE_MAX 31 |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 65 | #define PLDM_MAX_TYPES 64 |
| 66 | #define PLDM_MAX_CMDS_PER_TYPE 256 |
| 67 | |
| 68 | /* Message payload lengths */ |
| 69 | #define PLDM_GET_COMMANDS_REQ_BYTES 5 |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 70 | #define PLDM_GET_VERSION_REQ_BYTES 6 |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 71 | |
| 72 | /* Response lengths are inclusive of completion code */ |
| 73 | #define PLDM_GET_TYPES_RESP_BYTES 9 |
| 74 | #define PLDM_GET_COMMANDS_RESP_BYTES 33 |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 75 | /* Response data has only one version and does not contain the checksum */ |
| 76 | #define PLDM_GET_VERSION_RESP_BYTES 10 |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 77 | |
| 78 | /** @struct pldm_msg_hdr |
| 79 | * |
| 80 | * Structure representing PLDM message header fields |
| 81 | */ |
| 82 | struct pldm_msg_hdr { |
| 83 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 84 | uint8_t instance_id : 5; //!< Instance ID |
| 85 | uint8_t reserved : 1; //!< Reserved |
| 86 | uint8_t datagram : 1; //!< Datagram bit |
| 87 | uint8_t request : 1; //!< Request bit |
| 88 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 89 | uint8_t request : 1; //!< Request bit |
| 90 | uint8_t datagram : 1; //!< Datagram bit |
| 91 | uint8_t reserved : 1; //!< Reserved |
| 92 | uint8_t instance_id : 5; //!< Instance ID |
| 93 | #endif |
| 94 | |
| 95 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 96 | uint8_t type : 6; //!< PLDM type |
| 97 | uint8_t header_ver : 2; //!< Header version |
| 98 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 99 | uint8_t header_ver : 2; //!< Header version |
| 100 | uint8_t type : 6; //!< PLDM type |
| 101 | #endif |
| 102 | uint8_t command; //!< PLDM command code |
| 103 | } __attribute__((packed)); |
| 104 | |
| 105 | /** @struct pldm_msg_payload |
| 106 | * |
| 107 | * Structure representing PLDM message payload |
| 108 | */ |
| 109 | struct pldm_msg_payload { |
| 110 | uint8_t *payload; //!< Pointer to PLDM message payload |
| 111 | size_t payload_length; //!< PLDM message payload's length in bytes |
| 112 | } __attribute__((packed)); |
| 113 | |
| 114 | /** @struct pldm_msg |
| 115 | * |
| 116 | * Structure representing PLDM message |
| 117 | */ |
| 118 | struct pldm_msg { |
| 119 | struct pldm_msg_hdr hdr; //!< PLDM message header |
| 120 | struct pldm_msg_payload body; //!< PLDM message payload |
| 121 | } __attribute__((packed)); |
| 122 | |
Tom Joseph | 4125104 | 2019-02-07 16:17:07 +0530 | [diff] [blame] | 123 | /** @struct pldm_header_info |
| 124 | * |
| 125 | * The information needed to prepare PLDM header and this is passed to the |
| 126 | * pack_pldm_header and unpack_pldm_header API. |
| 127 | */ |
| 128 | struct pldm_header_info { |
| 129 | MessageType msg_type; //!< PLDM message type |
| 130 | uint8_t instance; //!< PLDM instance id |
| 131 | uint8_t pldm_type; //!< PLDM type |
| 132 | uint8_t command; //!< PLDM command code |
| 133 | uint8_t completion_code; //!< PLDM completion code, applies for response |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * @brief Populate the PLDM message with the PLDM header.The caller of this API |
| 138 | * allocates buffer for the PLDM header when forming the PLDM message. |
| 139 | * The buffer is passed to this API to pack the PLDM header. |
| 140 | * |
| 141 | * @param[in] hdr - Pointer to the PLDM header information |
| 142 | * @param[out] msg - Pointer to PLDM message header |
| 143 | * |
| 144 | * @return 0 on success, otherwise PLDM error codes. |
| 145 | */ |
| 146 | int pack_pldm_header(const struct pldm_header_info *hdr, |
| 147 | struct pldm_msg_hdr *msg); |
| 148 | |
| 149 | /** |
| 150 | * @brief Unpack the PLDM header from the PLDM message. |
| 151 | * |
| 152 | * @param[in] msg - Pointer to the PLDM message header |
| 153 | * @param[out] hdr - Pointer to the PLDM header information |
| 154 | * |
| 155 | * @return 0 on success, otherwise PLDM error codes. |
| 156 | */ |
| 157 | int unpack_pldm_header(const struct pldm_msg_hdr *msg, |
| 158 | struct pldm_header_info *hdr); |
| 159 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 160 | /* Requester */ |
| 161 | |
| 162 | /* GetPLDMTypes */ |
| 163 | |
| 164 | /** @brief Create a PLDM request message for GetPLDMTypes |
| 165 | * |
| 166 | * @param[in] instance_id - Message's instance id |
| 167 | * @param[in,out] msg - Message will be written to this |
| 168 | * @return pldm_completion_codes |
| 169 | * @note Caller is responsible for memory alloc and dealloc of param |
| 170 | * 'msg.body.payload' |
| 171 | */ |
| 172 | int encode_get_types_req(uint8_t instance_id, struct pldm_msg *msg); |
| 173 | |
| 174 | /** @brief Decode a GetPLDMTypes response message |
| 175 | * |
| 176 | * @param[in] msg - Response message payload |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 177 | * @param[out] completion_code - Pointer to response msg's PLDM completion code |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 178 | * @param[out] types - pointer to array bitfield8_t[8] containing supported |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 179 | * types (MAX_TYPES/8) = 8), as per DSP0240 |
| 180 | * @return pldm_completion_codes |
| 181 | */ |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 182 | int decode_get_types_resp(const struct pldm_msg_payload *msg, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 183 | uint8_t *completion_code, bitfield8_t *types); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 184 | |
| 185 | /* GetPLDMCommands */ |
| 186 | |
| 187 | /** @brief Create a PLDM request message for GetPLDMCommands |
| 188 | * |
| 189 | * @param[in] instance_id - Message's instance id |
| 190 | * @param[in] type - PLDM Type |
| 191 | * @param[in] version - Version for PLDM Type |
| 192 | * @param[in,out] msg - Message will be written to this |
| 193 | * @return pldm_completion_codes |
| 194 | * @note Caller is responsible for memory alloc and dealloc of param |
| 195 | * 'msg.body.payload' |
| 196 | */ |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 197 | int encode_get_commands_req(uint8_t instance_id, uint8_t type, ver32_t version, |
| 198 | struct pldm_msg *msg); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 199 | |
| 200 | /** @brief Decode a GetPLDMCommands response message |
| 201 | * |
| 202 | * @param[in] msg - Response message payload |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 203 | * @param[out] completion_code - Pointer to response msg's PLDM completion code |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 204 | * @param[in] commands - pointer to array bitfield8_t[32] containing supported |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 205 | * commands (PLDM_MAX_CMDS_PER_TYPE/8) = 32), as per DSP0240 |
| 206 | * @return pldm_completion_codes |
| 207 | */ |
| 208 | int decode_get_commands_resp(const struct pldm_msg_payload *msg, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 209 | uint8_t *completion_code, bitfield8_t *commands); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 210 | |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 211 | /* GetPLDMVersion */ |
| 212 | |
| 213 | /** @brief Create a PLDM request for GetPLDMVersion |
| 214 | * |
| 215 | * @param[in] instance_id - Message's instance id |
| 216 | * @param[in] transfer_handle - Handle to identify PLDM version data transfer. |
| 217 | * This handle is ignored by the responder when the |
| 218 | * transferop_flag is set to getFirstPart. |
| 219 | * @param[in] transfer_opflag - flag to indicate whether it is start of |
| 220 | * transfer |
| 221 | * @param[in] type - PLDM Type for which version is requested |
| 222 | * @param[in,out] msg - Message will be written to this |
| 223 | * @return pldm_completion_codes |
| 224 | * @note Caller is responsible for memory alloc and dealloc of param |
| 225 | * 'msg.body.payload' |
| 226 | */ |
| 227 | int encode_get_version_req(uint8_t instance_id, uint32_t transfer_handle, |
| 228 | uint8_t transfer_opflag, uint8_t type, |
| 229 | struct pldm_msg *msg); |
| 230 | |
| 231 | /** @brief Decode a GetPLDMVersion response message |
| 232 | * |
| 233 | * @param[in] msg - Response message payload |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 234 | * @param[out] completion_code - Pointer to response msg's PLDM completion code |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 235 | * @param[out] next_transfer_handle - the next handle for the next part of data |
| 236 | * @param[out] transfer_flag - flag to indicate the part of data |
| 237 | * @return pldm_completion_codes |
| 238 | */ |
| 239 | int decode_get_version_resp(const struct pldm_msg_payload *msg, |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 240 | uint8_t *completion_code, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 241 | uint32_t *next_transfer_handle, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 242 | uint8_t *transfer_flag, ver32_t *version); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 243 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 244 | /* Responder */ |
| 245 | |
| 246 | /* GetPLDMTypes */ |
| 247 | |
| 248 | /** @brief Create a PLDM response message for GetPLDMTypes |
| 249 | * |
| 250 | * @param[in] instance_id - Message's instance id |
| 251 | * @param[in] completion_code - PLDM completion code |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 252 | * @param[in] types - pointer to array bitfield8_t[8] containing supported |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 253 | * types (MAX_TYPES/8) = 8), as per DSP0240 |
| 254 | * @param[in,out] msg - Message will be written to this |
| 255 | * @return pldm_completion_codes |
| 256 | * @note Caller is responsible for memory alloc and dealloc of param |
| 257 | * 'msg.body.payload' |
| 258 | */ |
| 259 | int encode_get_types_resp(uint8_t instance_id, uint8_t completion_code, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 260 | const bitfield8_t *types, struct pldm_msg *msg); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 261 | |
| 262 | /* GetPLDMCommands */ |
| 263 | |
| 264 | /** @brief Decode GetPLDMCommands' request data |
| 265 | * |
| 266 | * @param[in] msg - Request message payload |
| 267 | * @param[out] type - PLDM Type |
| 268 | * @param[out] version - Version for PLDM Type |
| 269 | * @return pldm_completion_codes |
| 270 | */ |
| 271 | int decode_get_commands_req(const struct pldm_msg_payload *msg, uint8_t *type, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 272 | ver32_t *version); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 273 | |
| 274 | /** @brief Create a PLDM response message for GetPLDMCommands |
| 275 | * |
| 276 | * @param[in] instance_id - Message's instance id |
| 277 | * @param[in] completion_code - PLDM completion code |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 278 | * @param[in] commands - pointer to array bitfield8_t[32] containing supported |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 279 | * commands (PLDM_MAX_CMDS_PER_TYPE/8) = 32), as per DSP0240 |
| 280 | * @param[in,out] msg - Message will be written to this |
| 281 | * @return pldm_completion_codes |
| 282 | * @note Caller is responsible for memory alloc and dealloc of param |
| 283 | * 'msg.body.payload' |
| 284 | */ |
| 285 | int encode_get_commands_resp(uint8_t instance_id, uint8_t completion_code, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 286 | const bitfield8_t *commands, struct pldm_msg *msg); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 287 | |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 288 | /* GetPLDMVersion */ |
| 289 | |
| 290 | /** @brief Create a PLDM response for GetPLDMVersion |
| 291 | * |
| 292 | * @param[in] instance_id - Message's instance id |
| 293 | * @param[in] completion_code - PLDM completion code |
| 294 | * @param[in] next_transfer_handle - Handle to identify next portion of |
| 295 | * data transfer |
| 296 | * @param[in] transfer_flag - Represents the part of transfer |
| 297 | * @param[in] version_data - the version data |
| 298 | * @param[in] version_size - size of version data |
| 299 | * @param[in,out] msg - Message will be written to this |
| 300 | * @return pldm_completion_codes |
| 301 | * @note Caller is responsible for memory alloc and dealloc of param |
| 302 | * 'msg.body.payload' |
| 303 | */ |
| 304 | int encode_get_version_resp(uint8_t instance_id, uint8_t completion_code, |
| 305 | uint32_t next_transfer_handle, |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 306 | uint8_t transfer_flag, const ver32_t *version_data, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 307 | size_t version_size, struct pldm_msg *msg); |
| 308 | |
| 309 | /** @brief Decode a GetPLDMVersion request message |
| 310 | * |
| 311 | * @param[in] msg - Request message payload |
| 312 | * @param[out] transfer_handle - the handle of data |
| 313 | * @param[out] transfer_opflag - Transfer Flag |
| 314 | * @param[out] type - PLDM type for which version is requested |
| 315 | * @return pldm_completion_codes |
| 316 | */ |
| 317 | int decode_get_version_req(const struct pldm_msg_payload *msg, |
| 318 | uint32_t *transfer_handle, uint8_t *transfer_opflag, |
| 319 | uint8_t *type); |
| 320 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 321 | #ifdef __cplusplus |
| 322 | } |
| 323 | #endif |
| 324 | |
| 325 | #endif /* BASE_H */ |