Tom Joseph | ff4642c | 2019-04-10 13:41:32 +0530 | [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 | |
| 12 | #include "pldm_types.h" |
| 13 | |
| 14 | /** @brief PLDM Types |
| 15 | */ |
| 16 | enum pldm_supported_types { |
| 17 | PLDM_BASE = 0x00, |
| 18 | }; |
| 19 | |
| 20 | /** @brief PLDM Commands |
| 21 | */ |
| 22 | enum pldm_supported_commands { |
| 23 | PLDM_GET_PLDM_VERSION = 0x3, |
| 24 | PLDM_GET_PLDM_TYPES = 0x4, |
| 25 | PLDM_GET_PLDM_COMMANDS = 0x5 |
| 26 | }; |
| 27 | |
| 28 | /** @brief PLDM base codes |
| 29 | */ |
| 30 | enum pldm_completion_codes { |
| 31 | PLDM_SUCCESS = 0x00, |
| 32 | PLDM_ERROR = 0x01, |
| 33 | PLDM_ERROR_INVALID_DATA = 0x02, |
| 34 | PLDM_ERROR_INVALID_LENGTH = 0x03, |
| 35 | PLDM_ERROR_NOT_READY = 0x04, |
| 36 | PLDM_ERROR_UNSUPPORTED_PLDM_CMD = 0x05, |
| 37 | PLDM_ERROR_INVALID_PLDM_TYPE = 0x20 |
| 38 | }; |
| 39 | |
| 40 | enum transfer_op_flag { |
| 41 | PLDM_GET_NEXTPART = 0, |
| 42 | PLDM_GET_FIRSTPART = 1, |
| 43 | }; |
| 44 | |
| 45 | enum transfer_resp_flag { |
| 46 | PLDM_START = 0x01, |
| 47 | PLDM_MIDDLE = 0x02, |
| 48 | PLDM_END = 0x04, |
| 49 | PLDM_START_AND_END = 0x05, |
| 50 | }; |
| 51 | |
| 52 | /** @enum MessageType |
| 53 | * |
| 54 | * The different message types supported by the PLDM specification. |
| 55 | */ |
| 56 | typedef enum { |
| 57 | PLDM_RESPONSE, //!< PLDM response |
| 58 | PLDM_REQUEST, //!< PLDM request |
| 59 | PLDM_RESERVED, //!< Reserved |
| 60 | PLDM_ASYNC_REQUEST_NOTIFY, //!< Unacknowledged PLDM request messages |
| 61 | } MessageType; |
| 62 | |
| 63 | #define PLDM_INSTANCE_MAX 31 |
| 64 | #define PLDM_MAX_TYPES 64 |
| 65 | #define PLDM_MAX_CMDS_PER_TYPE 256 |
| 66 | |
| 67 | /* Message payload lengths */ |
| 68 | #define PLDM_GET_COMMANDS_REQ_BYTES 5 |
| 69 | #define PLDM_GET_VERSION_REQ_BYTES 6 |
| 70 | |
| 71 | /* Response lengths are inclusive of completion code */ |
| 72 | #define PLDM_GET_TYPES_RESP_BYTES 9 |
| 73 | #define PLDM_GET_COMMANDS_RESP_BYTES 33 |
| 74 | /* Response data has only one version and does not contain the checksum */ |
| 75 | #define PLDM_GET_VERSION_RESP_BYTES 10 |
| 76 | |
| 77 | /** @struct pldm_msg_hdr |
| 78 | * |
| 79 | * Structure representing PLDM message header fields |
| 80 | */ |
| 81 | struct pldm_msg_hdr { |
| 82 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 83 | uint8_t instance_id : 5; //!< Instance ID |
| 84 | uint8_t reserved : 1; //!< Reserved |
| 85 | uint8_t datagram : 1; //!< Datagram bit |
| 86 | uint8_t request : 1; //!< Request bit |
| 87 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 88 | uint8_t request : 1; //!< Request bit |
| 89 | uint8_t datagram : 1; //!< Datagram bit |
| 90 | uint8_t reserved : 1; //!< Reserved |
| 91 | uint8_t instance_id : 5; //!< Instance ID |
| 92 | #endif |
| 93 | |
| 94 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 95 | uint8_t type : 6; //!< PLDM type |
| 96 | uint8_t header_ver : 2; //!< Header version |
| 97 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 98 | uint8_t header_ver : 2; //!< Header version |
| 99 | uint8_t type : 6; //!< PLDM type |
| 100 | #endif |
| 101 | uint8_t command; //!< PLDM command code |
| 102 | } __attribute__((packed)); |
| 103 | |
| 104 | /** @struct pldm_msg |
| 105 | * |
| 106 | * Structure representing PLDM message |
| 107 | */ |
| 108 | struct pldm_msg { |
| 109 | struct pldm_msg_hdr hdr; //!< PLDM message header |
| 110 | uint8_t payload[1]; //!< Starting byte of the message payload |
| 111 | } __attribute__((packed)); |
| 112 | |
| 113 | /** @struct pldm_header_info |
| 114 | * |
| 115 | * The information needed to prepare PLDM header and this is passed to the |
| 116 | * pack_pldm_header and unpack_pldm_header API. |
| 117 | */ |
| 118 | struct pldm_header_info { |
| 119 | MessageType msg_type; //!< PLDM message type |
| 120 | uint8_t instance; //!< PLDM instance id |
| 121 | uint8_t pldm_type; //!< PLDM type |
| 122 | uint8_t command; //!< PLDM command code |
| 123 | uint8_t completion_code; //!< PLDM completion code, applies for response |
| 124 | }; |
| 125 | |
| 126 | /** |
| 127 | * @brief Populate the PLDM message with the PLDM header.The caller of this API |
| 128 | * allocates buffer for the PLDM header when forming the PLDM message. |
| 129 | * The buffer is passed to this API to pack the PLDM header. |
| 130 | * |
| 131 | * @param[in] hdr - Pointer to the PLDM header information |
| 132 | * @param[out] msg - Pointer to PLDM message header |
| 133 | * |
| 134 | * @return 0 on success, otherwise PLDM error codes. |
| 135 | */ |
| 136 | int pack_pldm_header(const struct pldm_header_info *hdr, |
| 137 | struct pldm_msg_hdr *msg); |
| 138 | |
| 139 | /** |
| 140 | * @brief Unpack the PLDM header from the PLDM message. |
| 141 | * |
| 142 | * @param[in] msg - Pointer to the PLDM message header |
| 143 | * @param[out] hdr - Pointer to the PLDM header information |
| 144 | * |
| 145 | * @return 0 on success, otherwise PLDM error codes. |
| 146 | */ |
| 147 | int unpack_pldm_header(const struct pldm_msg_hdr *msg, |
| 148 | struct pldm_header_info *hdr); |
| 149 | |
| 150 | #ifdef __cplusplus |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | #endif /* BASE_H */ |