* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / lib / src / xran_common.h
1 /******************************************************************************
2 *
3 *   Copyright (c) 2020 Intel.
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 *
17 *******************************************************************************/
18
19 /**
20  * @brief XRAN layer common functionality for both lls-CU and RU as well as C-plane and
21  *    U-plane
22  * @file xran_common.h
23  * @ingroup group_source_xran
24  * @author Intel Corporation
25  **/
26
27 #ifndef _XRAN_COMMON_H_
28 #define _XRAN_COMMON_H_
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38
39 #include <rte_common.h>
40 #include <rte_mbuf.h>
41 #include <rte_timer.h>
42 #include <rte_log.h>
43
44 #include "xran_fh_o_du.h"
45 #include "xran_pkt_up.h"
46 #include "xran_cp_api.h"
47 #include "xran_dev.h"
48 #include "xran_pkt.h"
49
50
51 extern uint64_t interval_us;
52 #define O_DU 0
53 #define O_RU 1
54
55 #define N_SC_PER_PRB 12
56 #define MAX_N_FULLBAND_SC 273
57 #define N_SYM_PER_SLOT 14
58 #define SUBFRAME_DURATION_US 1000
59 #define SLOTNUM_PER_SUBFRAME(interval)       (SUBFRAME_DURATION_US/(interval))
60 #define SUBFRAMES_PER_SYSTEMFRAME  10
61 #define SLOTS_PER_SYSTEMFRAME(interval) ((SLOTNUM_PER_SUBFRAME(interval))*SUBFRAMES_PER_SYSTEMFRAME)
62
63 /* PRACH data samples are 32 bits wide, 16bits for I and 16bits for Q. Each packet contains 839 samples for long sequence or 144 for short sequence. The payload length is 840*16*2/8 octets.*/
64 #ifdef FCN_1_2_6_EARLIER
65 #define PRACH_PLAYBACK_BUFFER_BYTES (144*4L)
66 #else
67 #define PRACH_PLAYBACK_BUFFER_BYTES (840*4L)
68 #endif
69 #define PRACH_SRS_BUFFER_BYTES (144*14*4L)
70
71 /**<  this is the configuration of M-plane */
72 #define XRAN_MAX_NUM_SECTIONS       (N_SYM_PER_SLOT* (XRAN_MAX_ANTENNA_NR*2) + XRAN_MAX_ANT_ARRAY_ELM_NR)
73
74 #define    XRAN_PAYLOAD_1_RB_SZ(iqWidth) (((iqWidth == 0) || (iqWidth == 16)) ? \
75                 (N_SC_PER_PRB*(MAX_IQ_BIT_WIDTH/8)*2) : (3 * iqWidth + 1))
76
77 #define XRAN_MAX_MBUF_LEN (13168 + XRAN_MAX_SECTIONS_PER_SYM* (RTE_PKTMBUF_HEADROOM + sizeof(struct rte_ether_hdr) + sizeof(struct xran_ecpri_hdr) + sizeof(struct radio_app_common_hdr) + sizeof(struct data_section_hdr)))
78 #define NSEC_PER_SEC 1000000000L
79 #define TIMER_RESOLUTION_CYCLES 1596*1 /* 1us */
80 #define XRAN_RING_SIZE  512 /*4*14*8 pow of 2 */
81 #define XRAN_NAME_MAX_LEN       (64)
82 #define XRAN_RING_NUM       (3)
83
84 #define XranDiffSymIdx(prevSymIdx, currSymIdx, numTotalSymIdx)  ((prevSymIdx > currSymIdx) ? ((currSymIdx + numTotalSymIdx) - prevSymIdx) : (currSymIdx - prevSymIdx))
85
86 #define XRAN_MLOG_VAR 0 /**< enable debug variables to mlog */
87
88
89 #define DIV_ROUND_OFFSET(X,Y) ( X/Y + ((X%Y)?1:0) )
90
91 #define MAX_NUM_OF_XRAN_CTX          (2)
92 #define XranIncrementCtx(ctx)                             ((ctx >= (MAX_NUM_OF_XRAN_CTX-1)) ? 0 : (ctx+1))
93 #define XranDecrementCtx(ctx)                             ((ctx == 0) ? (MAX_NUM_OF_XRAN_CTX-1) : (ctx-1))
94
95 #define MAX_NUM_OF_DPDK_TIMERS       (10)
96 #define DpdkTimerIncrementCtx(ctx)           ((ctx >= (MAX_NUM_OF_DPDK_TIMERS-1)) ? 0 : (ctx+1))
97 #define DpdkTimerDecrementCtx(ctx)           ((ctx == 0) ? (MAX_NUM_OF_DPDK_TIMERS-1) : (ctx-1))
98
99 /* Callback function to send mbuf to the ring */
100 typedef int (*xran_ethdi_mbuf_send_fn)(struct rte_mbuf *mb, uint16_t ethertype, uint16_t vf_id);
101
102
103 #define XranIncrementJob(i)                  ((i >= (XRAN_SYM_JOB_SIZE-1)) ? 0 : (i+1))
104
105 /** Worker task function type */
106 typedef int32_t (*worker_task_fn)(void*);
107
108 /** worker thread context structure */
109 struct xran_worker_th_ctx {
110     pthread_t      *pThread;
111
112     struct sched_param sched_param;
113     char      worker_name[32];
114     int32_t   worker_id;
115     uint64_t  worker_core_id;
116     int32_t   worker_policy;
117     int32_t   worker_status;
118
119     /* task */
120     worker_task_fn task_func;
121     void* task_arg;
122 };
123
124 struct xran_sectioninfo_db {
125     uint32_t    cur_index;  /**< Current index to store for this eAXC */
126     struct xran_section_info list[XRAN_MAX_NUM_SECTIONS]; /**< The array of section information */
127 };
128
129 int32_t xran_generic_worker_thread(void *args);
130
131 int process_mbuf(struct rte_mbuf *pkt, void* handle, struct xran_eaxc_info *p_cid);
132 int process_mbuf_batch(struct rte_mbuf* pkt[], void* handle, int16_t num, struct xran_eaxc_info *p_cid, uint32_t* ret);
133 int process_ring(struct rte_ring *r,  uint16_t ring_id, uint16_t q_id);
134 int ring_processing_thread(void *args);
135 int packets_dump_thread(void *args);
136 // Support for 1-way eCPRI delay measurement per section 3.2.4.6 of eCPRI Specification V2.0
137 int32_t xran_ecpri_port_update_required(struct xran_io_cfg * cfg, uint16_t port_id);
138 int xran_ecpri_one_way_delay_measurement_transmitter(uint16_t port_id, void* handle);
139 int xran_generate_delay_meas(uint16_t port_id, void* handle, uint8_t actionType, uint8_t MeasurementID );
140 int process_delay_meas(struct rte_mbuf *pkt,  void* handle, uint16_t port_id);
141 int xran_process_delmeas_request(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
142 int xran_process_delmeas_request_w_fup(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
143 int xran_process_delmeas_response(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
144 int xran_process_delmeas_rem_request(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
145 int xran_process_delmeas_rem_request_w_fup(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
146 int xran_process_delmeas_follow_up(struct rte_mbuf *pkt, void* handle, struct xran_ecpri_del_meas_pkt*, uint16_t port_id);
147 void xran_initialize_ecpri_del_meas_port(struct xran_ecpri_del_meas_cmn* pCmn, struct xran_ecpri_del_meas_port* pPort,uint16_t full_init);
148
149 int send_symbol_mult_section_ex(void* handle,
150                 enum xran_pkt_dir direction,
151                 uint16_t section_id,
152                 struct rte_mbuf *mb,
153                 uint8_t *data,
154                 uint8_t compMeth,
155                 uint8_t iqWidth,
156                 const enum xran_input_byte_order iq_buf_byte_order,
157                 uint8_t frame_id,
158                 uint8_t subframe_id,
159                 uint8_t slot_id,
160                 uint8_t symbol_no,
161                 int prb_start,
162                 int prb_num,
163                 uint8_t CC_ID,
164                 uint8_t RU_Port_ID,
165                 uint8_t seq_id);
166
167 int send_symbol_ex(void* handle,
168                 enum xran_pkt_dir direction,
169                 uint16_t section_id,
170                 struct rte_mbuf *mb,
171                 uint8_t *data,
172                 uint8_t compMeth,
173                 uint8_t iqWidth,
174                 const enum xran_input_byte_order iq_buf_byte_order,
175                 uint8_t frame_id,
176                 uint8_t subframe_id,
177                 uint8_t slot_id,
178                 uint8_t symbol_no,
179                 int prb_start,
180                 int prb_num,
181                 uint8_t CC_ID,
182                 uint8_t RU_Port_ID,
183                 uint8_t seq_id);
184
185 int32_t prepare_symbol_ex(enum xran_pkt_dir direction,
186                 uint16_t section_id,
187                 struct rte_mbuf *mb,
188                 uint8_t *data,
189                 uint8_t     compMeth,
190                 uint8_t     iqWidth,
191                 const enum xran_input_byte_order iq_buf_byte_order,
192                 uint8_t frame_id,
193                 uint8_t subframe_id,
194                 uint8_t slot_id,
195                 uint8_t symbol_no,
196                 int prb_start,
197                 int prb_num,
198                 uint8_t CC_ID,
199                 uint8_t RU_Port_ID,
200                 uint8_t seq_id,
201                 uint32_t do_copy,
202                 enum xran_comp_hdr_type staticEn,
203                 uint16_t num_sections,
204                 uint16_t iq_buffer_offset);
205 int32_t prepare_sf_slot_sym (enum xran_pkt_dir direction,
206                 uint8_t frame_id,
207                 uint8_t subframe_id,
208                 uint8_t slot_id,
209                 uint8_t symbol_no,
210                 struct xran_up_pkt_gen_params *xp);
211
212 static inline int32_t prepare_symbol_opt(enum xran_pkt_dir direction,
213                 uint16_t section_id,
214                 struct rte_mbuf *mb,
215                 struct rb_map *data,
216                 uint8_t     compMeth,
217                 uint8_t     iqWidth,
218                 const enum xran_input_byte_order iq_buf_byte_order,
219                 int prb_start,
220                 int prb_num,
221                 uint8_t CC_ID,
222                 uint8_t RU_Port_ID,
223                 uint8_t seq_id,
224                 uint32_t do_copy,
225                 struct xran_up_pkt_gen_params *xp,
226                 enum xran_comp_hdr_type staticEn);
227
228
229 int send_cpmsg(void *pHandle, struct rte_mbuf *mbuf,struct xran_cp_gen_params *params,
230                 struct xran_section_gen_info *sect_geninfo, uint8_t cc_id, uint8_t ru_port_id, uint8_t seq_id);
231
232 int32_t generate_cpmsg_dlul(void *pHandle, struct xran_cp_gen_params *params, struct xran_section_gen_info *sect_geninfo, struct rte_mbuf *mbuf,
233     enum xran_pkt_dir dir, uint8_t frame_id, uint8_t subframe_id, uint8_t slot_id,
234     uint8_t startsym, uint8_t numsym, uint16_t prb_start, uint16_t prb_num,int16_t iq_buffer_offset, int16_t iq_buffer_len,
235     uint16_t beam_id, uint8_t cc_id, uint8_t ru_port_id, uint8_t comp_method, uint8_t iqWidth,  uint8_t seq_id, uint8_t symInc);
236
237 int generate_cpmsg_prach(void *pHandle, struct xran_cp_gen_params *params, struct xran_section_gen_info *sect_geninfo, struct rte_mbuf *mbuf, struct xran_device_ctx *pxran_lib_ctx,
238                 uint8_t frame_id, uint8_t subframe_id, uint8_t slot_id, int tti,
239                 uint16_t beam_id, uint8_t cc_id, uint8_t prach_port_id, uint16_t occasionid, uint8_t seq_id);
240
241 struct xran_eaxcid_config *xran_get_conf_eAxC(void *pHandle);
242 int xran_register_cb_mbuf2ring(xran_ethdi_mbuf_send_fn mbuf_send_cp, xran_ethdi_mbuf_send_fn mbuf_send_up);
243
244 //uint16_t xran_alloc_sectionid(void *pHandle, uint8_t dir, uint8_t cc_id, uint8_t ant_id, uint8_t slot_id);
245 uint8_t xran_get_seqid(void *pHandle, uint8_t dir, uint8_t cc_id, uint8_t ant_id, uint8_t slot_id);
246 int32_t ring_processing_func(void* arg);
247 int xran_init_prach(struct xran_fh_config* pConf, struct xran_device_ctx * p_xran_dev_ctx, enum xran_ran_tech xran_tech);
248 void xran_updateSfnSecStart(void);
249 uint32_t xran_slotid_convert(uint16_t slot_id, uint16_t dir);
250
251 uint16_t xran_map_ecpriRtcid_to_vf(struct xran_device_ctx *p_dev_ctx,  int32_t dir, int32_t cc_id, int32_t ru_port_id);
252 uint16_t xran_map_ecpriPcid_to_vf(struct xran_device_ctx *p_dev_ctx, int32_t dir, int32_t cc_id, int32_t ru_port_id);
253
254 #ifdef __cplusplus
255 }
256 #endif
257
258 #endif
259