OpenBIC
OpenSource Bridge-IC
mctp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _MCTP_H
18#define _MCTP_H
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24#include <stdint.h>
25#include <sys/printk.h>
26#include <zephyr.h>
27
28#define MCTP_DEBUG 1
29
30#define MCTP_SUCCESS 0
31#define MCTP_ERROR 1
32
33#define MCTP_RESPONSE 0
34#define MCTP_REQUEST 1
35
36#define MCTP_TX_QUEUE_SIZE 16
37
38#define MSG_ASSEMBLY_BUF_SIZE 1024
39
40#define MCTP_RX_TASK_STACK_SIZE 4096
41#define MCTP_TX_TASK_STACK_SIZE 2048
42#define MCTP_TASK_NAME_LEN 32
43
44#define MCTP_DEFAULT_ENDPOINT 0x0A
45#define MCTP_NULL_EID 0x00
46
47#define MCTP_DEFAULT_MSG_MAX_SIZE 64
48#define MCTP_TRANSPORT_HEADER_SIZE 4
49#define MCTP_MEDIUM_META_SIZE_SMBUS 3
50#define MCTP_PEC_SIZE 1 /* SMBUS/I3C */
51#define MCTP_META_INFO_SIZE (MCTP_TRANSPORT_HEADER_SIZE + MCTP_PEC_SIZE)
52
53#define MCTP_MAX_MSG_TAG_NUM 8
54
55#define MCTP_HDR_HDR_VER 0x01
56#define MCTP_HDR_SEQ_MASK 0x03
57#define MCTP_HDR_TAG_MASK 0x07
58
59#define MCTP_POLL_TIME_MS 1
60
61#define MCTP_MSG_TYPE_SHIFT 0
62#define MCTP_MSG_TYPE_MASK 0x7F
63
64#define MCTP_IC_SHIFT 7
65#define MCTP_IC_MASK 0x80
66
67typedef enum {
77
78typedef enum {
85
86/* smbus extra medium data of endpoint */
87typedef struct _mctp_i3c_ext_params {
88 uint8_t addr; /* 8 bit address */
89 uint32_t dummy; // TODO: test only
91
92/* smbus extra medium data of endpoint */
93typedef struct _mctp_smbus_ext_params {
94 uint8_t addr; /* 8 bit address */
96
97/* mctp extra parameters prototype */
98typedef struct _mctp_ext_params {
99 /* mctp transport layer parameters */
100 uint8_t tag_owner;
101 uint8_t msg_tag;
102 uint8_t ep;
103
104 /* medium parameters */
106 union {
109 };
111
112/* mctp recevice data callback function prototype */
113/* ext_params shoule be bypass to mctp_send_msg if need */
114typedef uint8_t (*mctp_fn_cb)(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params ext_params);
115
116/* medium write/read function prototype */
117typedef uint16_t (*medium_tx)(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params ext_params);
118typedef uint16_t (*medium_rx)(void *mctp_p, uint8_t *buf, uint32_t len,
119 mctp_ext_params *ext_params);
120
121/* prototype for destitation endpoint resloved */
122typedef uint8_t (*endpoint_resolve)(uint8_t dest_endpoint, void **mctp_inst,
123 mctp_ext_params *ext_params);
124
125/* i3c config for mctp medium_conf */
126typedef struct _mctp_i3c_conf {
127 uint8_t bus;
128 uint8_t addr;
130
131/* smbus config for mctp medium_conf */
132typedef struct _mctp_smbus_conf {
133 uint8_t bus;
134 uint8_t addr;
136
137/* mctp medium conf */
138typedef union {
142
143/* mctp tx message struct */
144typedef struct __attribute__((aligned(4))) {
145 uint8_t is_bridge_packet;
146 uint8_t *buf;
147 uint16_t len;
148 mctp_ext_params ext_params;
149 struct k_msgq *evt_msgq;
151
152/* mctp main struct */
153typedef struct _mctp {
156 uint8_t endpoint;
157 uint16_t max_msg_size;
158
159 /* medium related */
163
164 /* get mctp route information by application layer */
166
167 /* read/write task */
170 struct k_thread rx_task_thread_data;
171 struct k_thread tx_task_thread_data;
176
177 /* write queue */
178 struct k_msgq mctp_tx_queue;
179
180 /* point to the rx message buffer that is assembling request/response */
181 struct {
182 uint8_t *buf;
183 uint16_t offset;
185
186 /* the callback when recevie mctp data */
188
189 /* for pldm instance id */
191 uint32_t pldm_inst_table; // 32 bits field for instance id
192
193 /* for cci_msg_tag */
194 uint8_t cci_msg_tag;
195
196 /* for ncsi instance id */
198 uint32_t ncsi_inst_table[8]; // 256 bits field for instance id
199
200 /* for MCTP msg tag */
201 uint8_t msg_tag;
203
204typedef struct _mctp_smbus_port {
210
211/* mctp route entry struct */
212typedef struct _mctp_route_entry {
213 uint8_t endpoint;
214 uint8_t bus; /* TODO: only consider smbus/i3c */
215 uint8_t addr; /* TODO: only consider smbus/i3c */
219
220typedef struct _mctp_msg_handler {
224
226mctp_port *plat_get_mctp_port(uint8_t index);
227
228/* public function */
229mctp *mctp_init(void);
230
231uint8_t mctp_deinit(mctp *mctp_inst);
232
233uint8_t mctp_set_medium_configure(mctp *mctp_inst, MCTP_MEDIUM_TYPE medium_type,
234 mctp_medium_conf medium_conf);
235
236/* medium_conf should be freed by application */
237uint8_t mctp_get_medium_configure(mctp *mctp_inst, MCTP_MEDIUM_TYPE *medium_type,
238 mctp_medium_conf *medium_conf);
239
240/* mctp service start */
241uint8_t mctp_start(mctp *mctp_inst);
242
243/* mctp service stop */
244uint8_t mctp_stop(mctp *mctp_inst);
245
246/* send message to destination endpoint */
247uint8_t mctp_send_msg(mctp *mctp_inst, uint8_t *buf, uint16_t len, mctp_ext_params ext_params);
248
249/* bridge message to destination endpoint */
250uint8_t mctp_bridge_msg(mctp *mctp_inst, uint8_t *buf, uint16_t len, mctp_ext_params ext_params);
251
252/* medium init/deinit */
253uint8_t mctp_smbus_init(mctp *mctp_inst, mctp_medium_conf medium_conf);
254uint8_t mctp_smbus_deinit(mctp *mctp_inst);
255uint8_t mctp_i3c_controller_init(mctp *mctp_instance, mctp_medium_conf medium_conf);
256uint8_t mctp_i3c_target_init(mctp *mctp_instance, mctp_medium_conf medium_conf);
257uint8_t mctp_i3c_deinit(mctp *mctp_instance);
258
259/* register endpoint resolve function */
260uint8_t mctp_reg_endpoint_resolve_func(mctp *mctp_inst, endpoint_resolve resolve_fn);
261
262bool get_mctp_info_by_eid(uint8_t port, mctp **mctp_inst, mctp_ext_params *ext_params);
263uint8_t get_mctp_info(uint8_t dest_endpoint, mctp **mctp_inst, mctp_ext_params *ext_params);
264
265/* register callback function when the mctp message is received */
266uint8_t mctp_reg_msg_rx_func(mctp *mctp_inst, mctp_fn_cb rx_cb);
267
268mctp *pal_get_mctp(uint8_t mctp_medium_type, uint8_t bus);
269int pal_get_target(uint8_t interface);
270int pal_get_medium_type(uint8_t interface);
271uint8_t plat_get_eid();
273mctp *pal_find_mctp_by_bus(uint8_t bus);
275
276bool pal_is_need_mctp_interval(mctp *mctp_inst);
277int pal_get_mctp_interval_ms(mctp *mctp_inst);
278
279#ifdef __cplusplus
280}
281#endif
282
283#endif /* _MCTP_H */
bool get_mctp_info_by_eid(uint8_t port, mctp **mctp_inst, mctp_ext_params *ext_params)
Definition: mctp.c:648
uint8_t plat_get_eid()
Definition: mctp.c:419
struct _mctp_msg_handler mctp_msg_handler
uint8_t mctp_stop(mctp *mctp_inst)
Definition: mctp.c:489
uint8_t mctp_reg_endpoint_resolve_func(mctp *mctp_inst, endpoint_resolve resolve_fn)
Definition: mctp.c:625
mctp_port * pal_find_mctp_port_by_channel_target(uint8_t target)
Definition: mctp.c:699
mctp_tx_msg
Definition: mctp.h:150
uint8_t mctp_start(mctp *mctp_inst)
Definition: mctp.c:512
int pal_get_medium_type(uint8_t interface)
Definition: plat_mctp.c:85
uint8_t(* endpoint_resolve)(uint8_t dest_endpoint, void **mctp_inst, mctp_ext_params *ext_params)
Definition: mctp.h:122
struct _mctp_smbus_port mctp_port
uint16_t(* medium_tx)(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params ext_params)
Definition: mctp.h:117
uint8_t mctp_reg_msg_rx_func(mctp *mctp_inst, mctp_fn_cb rx_cb)
Definition: mctp.c:634
MCTP_MEDIUM_TYPE
Definition: mctp.h:78
@ MCTP_MEDIUM_TYPE_TARGET_I3C
Definition: mctp.h:82
@ MCTP_MEDIUM_TYPE_MAX
Definition: mctp.h:83
@ MCTP_MEDIUM_TYPE_CONTROLLER_I3C
Definition: mctp.h:81
@ MCTP_MEDIUM_TYPE_UNKNOWN
Definition: mctp.h:79
@ MCTP_MEDIUM_TYPE_SMBUS
Definition: mctp.h:80
uint16_t(* medium_rx)(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params *ext_params)
Definition: mctp.h:118
#define MCTP_MAX_MSG_TAG_NUM
Definition: mctp.h:53
uint8_t mctp_get_medium_configure(mctp *mctp_inst, MCTP_MEDIUM_TYPE *medium_type, mctp_medium_conf *medium_conf)
Definition: mctp.c:477
uint8_t plat_get_mctp_port_count()
Definition: mctp.c:675
MCTP_MSG_TYPE
Definition: mctp.h:67
@ MCTP_MSG_TYPE_PLDM
Definition: mctp.h:69
@ MCTP_MSG_TYPE_ETH
Definition: mctp.h:71
@ MCTP_MSG_TYPE_NVME
Definition: mctp.h:72
@ MCTP_MSG_TYPE_CCI
Definition: mctp.h:73
@ MCTP_MSG_TYPE_NCSI
Definition: mctp.h:70
@ MCTP_MSG_TYPE_CTRL
Definition: mctp.h:68
@ MCTP_MSG_TYPE_VEN_DEF_PCI
Definition: mctp.h:74
@ MCTP_MSG_TYPE_VEN_DEF_IANA
Definition: mctp.h:75
uint8_t mctp_i3c_deinit(mctp *mctp_instance)
uint8_t mctp_send_msg(mctp *mctp_inst, uint8_t *buf, uint16_t len, mctp_ext_params ext_params)
Definition: mctp.c:616
#define MCTP_TASK_NAME_LEN
Definition: mctp.h:42
struct _mctp_smbus_ext_params mctp_smbus_ext_params
int pal_get_target(uint8_t interface)
Definition: plat_mctp.c:103
uint8_t mctp_deinit(mctp *mctp_inst)
Definition: mctp.c:442
mctp_port * plat_get_mctp_port(uint8_t index)
Definition: mctp.c:680
mctp * pal_get_mctp(uint8_t mctp_medium_type, uint8_t bus)
Definition: plat_mctp.c:121
uint8_t get_mctp_info(uint8_t dest_endpoint, mctp **mctp_inst, mctp_ext_params *ext_params)
Definition: mctp.c:643
mctp * mctp_init(void)
Definition: mctp.c:425
struct _mctp_smbus_conf mctp_smbus_conf
#define MCTP_TX_TASK_STACK_SIZE
Definition: mctp.h:41
struct _mctp_route_entry mctp_route_entry
uint8_t mctp_smbus_deinit(mctp *mctp_inst)
Definition: mctp_smbus.c:243
uint8_t mctp_bridge_msg(mctp *mctp_inst, uint8_t *buf, uint16_t len, mctp_ext_params ext_params)
Definition: mctp.c:607
struct _mctp_ext_params mctp_ext_params
uint8_t mctp_i3c_controller_init(mctp *mctp_instance, mctp_medium_conf medium_conf)
#define MCTP_RX_TASK_STACK_SIZE
Definition: mctp.h:40
bool pal_is_need_mctp_interval(mctp *mctp_inst)
Definition: mctp.c:46
mctp * pal_find_mctp_by_bus(uint8_t bus)
Definition: mctp.c:685
uint8_t mctp_set_medium_configure(mctp *mctp_inst, MCTP_MEDIUM_TYPE medium_type, mctp_medium_conf medium_conf)
Definition: mctp.c:457
uint8_t mctp_smbus_init(mctp *mctp_inst, mctp_medium_conf medium_conf)
Definition: mctp_smbus.c:232
struct _mctp mctp
struct _mctp_i3c_conf mctp_i3c_conf
struct _mctp_i3c_ext_params mctp_i3c_ext_params
uint8_t(* mctp_fn_cb)(void *mctp_p, uint8_t *buf, uint32_t len, mctp_ext_params ext_params)
Definition: mctp.h:114
int pal_get_mctp_interval_ms(mctp *mctp_inst)
Definition: mctp.c:51
int pal_find_bus_in_mctp_port(mctp_port *p)
Definition: mctp.c:656
uint8_t mctp_i3c_target_init(mctp *mctp_instance, mctp_medium_conf medium_conf)
Definition: pldm_smbios.h:61
Definition: mctp.h:98
MCTP_MEDIUM_TYPE type
Definition: mctp.h:105
mctp_smbus_ext_params smbus_ext_params
Definition: mctp.h:107
uint8_t tag_owner
Definition: mctp.h:100
mctp_i3c_ext_params i3c_ext_params
Definition: mctp.h:108
uint8_t ep
Definition: mctp.h:102
uint8_t msg_tag
Definition: mctp.h:101
Definition: mctp.h:126
uint8_t bus
Definition: mctp.h:127
uint8_t addr
Definition: mctp.h:128
Definition: mctp.h:87
uint8_t addr
Definition: mctp.h:88
uint32_t dummy
Definition: mctp.h:89
Definition: mctp.h:220
mctp_fn_cb msg_handler_cb
Definition: mctp.h:222
MCTP_MSG_TYPE type
Definition: mctp.h:221
Definition: mctp.h:212
bool set_endpoint
Definition: mctp.h:217
uint8_t dev_present_pin
Definition: mctp.h:216
uint8_t addr
Definition: mctp.h:215
uint8_t bus
Definition: mctp.h:214
uint8_t endpoint
Definition: mctp.h:213
Definition: mctp.h:132
uint8_t bus
Definition: mctp.h:133
uint8_t addr
Definition: mctp.h:134
Definition: mctp.h:93
uint8_t addr
Definition: mctp.h:94
Definition: mctp.h:204
mctp * mctp_inst
Definition: mctp.h:205
MCTP_MEDIUM_TYPE medium_type
Definition: mctp.h:207
uint8_t channel_target
Definition: mctp.h:206
mctp_medium_conf conf
Definition: mctp.h:208
Definition: mctp.h:153
uint8_t mctp_tx_task_name[MCTP_TASK_NAME_LEN]
Definition: mctp.h:175
k_tid_t mctp_tx_task_tid
Definition: mctp.h:169
mctp_fn_cb rx_cb
Definition: mctp.h:187
struct k_msgq mctp_tx_queue
Definition: mctp.h:178
medium_tx write_data
Definition: mctp.h:162
uint8_t cci_msg_tag
Definition: mctp.h:194
struct _mctp::@71 temp_msg_buf[MCTP_MAX_MSG_TAG_NUM][2]
uint8_t endpoint
Definition: mctp.h:156
medium_rx read_data
Definition: mctp.h:161
uint8_t msg_tag
Definition: mctp.h:201
uint32_t ncsi_inst_table[8]
Definition: mctp.h:198
struct k_thread tx_task_thread_data
Definition: mctp.h:171
struct k_thread rx_task_thread_data
Definition: mctp.h:170
k_tid_t mctp_rx_task_tid
Definition: mctp.h:168
uint8_t * buf
Definition: mctp.h:182
mctp_medium_conf medium_conf
Definition: mctp.h:160
endpoint_resolve ep_resolve
Definition: mctp.h:165
K_KERNEL_STACK_MEMBER(tx_task_stack_area, MCTP_TX_TASK_STACK_SIZE)
K_KERNEL_STACK_MEMBER(rx_task_stack_area, MCTP_RX_TASK_STACK_SIZE)
uint32_t pldm_inst_table
Definition: mctp.h:191
MCTP_MEDIUM_TYPE medium_type
Definition: mctp.h:155
uint8_t pldm_inst_id
Definition: mctp.h:190
uint8_t is_servcie_start
Definition: mctp.h:154
uint16_t offset
Definition: mctp.h:183
uint8_t ncsi_inst_id
Definition: mctp.h:197
uint8_t mctp_rx_task_name[MCTP_TASK_NAME_LEN]
Definition: mctp.h:174
uint16_t max_msg_size
Definition: mctp.h:157
Definition: mctp.h:138
mctp_i3c_conf i3c_conf
Definition: mctp.h:140
mctp_smbus_conf smbus_conf
Definition: mctp.h:139