* INTC Contribution to the O-RAN F Release for O-DU Low
[o-du/phy.git] / fhi_lib / lib / src / xran_dev.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 O-DU|O-RU device context
21  * @file xran_dev.h
22  * @ingroup group_source_xran
23  * @author Intel Corporation
24  **/
25
26 #ifndef _XRAN_DEV_H_
27 #define _XRAN_DEV_H_
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37
38 #include <rte_common.h>
39 #include <rte_mbuf.h>
40 #include <rte_timer.h>
41
42 #include "xran_fh_o_du.h"
43 #include "xran_prach_cfg.h"
44 #include "xran_up_api.h"
45 #include "xran_cp_api.h"
46
47 #define DIV_ROUND_OFFSET(X,Y) ( X/Y + ((X%Y)?1:0) )
48
49 #define MAX_NUM_OF_XRAN_CTX          (2)
50 #define MAX_CB_TIMER_CTX             (10*MAX_NUM_OF_XRAN_CTX)
51 #define MAX_TTI_TO_PHY_TIMER         (10)
52 #define XranIncrementCtx(ctx)                             ((ctx >= (MAX_NUM_OF_XRAN_CTX-1)) ? 0 : (ctx+1))
53 #define XranDecrementCtx(ctx)                             ((ctx == 0) ? (MAX_NUM_OF_XRAN_CTX-1) : (ctx-1))
54
55 #define MAX_NUM_OF_DPDK_TIMERS       (10)
56 #define DpdkTimerIncrementCtx(ctx)           ((ctx >= (MAX_NUM_OF_DPDK_TIMERS-1)) ? 0 : (ctx+1))
57 #define DpdkTimerDecrementCtx(ctx)           ((ctx == 0) ? (MAX_NUM_OF_DPDK_TIMERS-1) : (ctx-1))
58
59 enum xran_job_type_id {
60     XRAN_JOB_TYPE_OTA_CB   = 0,
61     XRAN_JOB_TYPE_CP_DL    = 1,
62     XRAN_JOB_TYPE_CP_UL    = 2,
63     XRAN_JOB_TYPE_DEADLINE = 3,
64     XRAN_JOB_TYPE_SYM_CB   = 4,
65     XRAN_JOB_TYPE_MAX
66 };
67
68 struct xran_timer_ctx {
69     uint32_t    tti_to_process;
70     uint32_t    ota_sym_idx;
71     uint16_t    xran_sfn_at_sec_start;
72     uint64_t    current_second;
73 };
74
75 #define XRAN_MAX_POOLS_PER_SECTOR_NR 10 /**< 2x(TX_OUT, RX_IN, PRACH_IN, SRS_IN, BFW_BUF) with C-plane */
76
77 typedef struct sectorHandleInfo
78 {
79     /**< Structure that contains the information to describe the
80      * instance i.e service type, virtual function, package Id etc..*/
81     uint16_t nIndex;
82     uint16_t nXranPort;
83     /* Unique ID of an handle shared between phy layer and library */
84     /**< number of antennas supported per link*/
85     uint32_t nBufferPoolIndex;
86     /**< Buffer poolIndex*/
87     struct rte_mempool * p_bufferPool[XRAN_MAX_POOLS_PER_SECTOR_NR];
88     uint32_t bufferPoolElmSz[XRAN_MAX_POOLS_PER_SECTOR_NR];
89     uint32_t bufferPoolNumElm[XRAN_MAX_POOLS_PER_SECTOR_NR];
90
91 }XranSectorHandleInfo, *PXranSectorHandleInfo;
92
93 typedef void (*XranSymCallbackFn)(struct rte_timer *tim, void* arg, void *p_dev_ctx);
94 typedef int32_t (*tx_sym_gen_fn)(void* pHandle, uint8_t ctx_id, uint32_t tti, int32_t start_cc, int32_t num_cc, int32_t start_ant,  int32_t num_ant, uint32_t frame_id,
95     uint32_t subframe_id, uint32_t slot_id, uint32_t sym_id, enum xran_comp_hdr_type compType, enum xran_pkt_dir direction,
96     uint16_t xran_port_id, PSECTION_DB_TYPE p_sec_db);
97
98 struct cb_elem_entry{
99     XranSymCallbackFn pSymCallback;
100     void *pSymCallbackTag;
101     void *p_dev_ctx;
102     LIST_ENTRY(cb_elem_entry) pointers;
103 };
104
105 /* Callback function to send mbuf to the ring */
106 typedef int (*xran_ethdi_mbuf_send_fn)(struct rte_mbuf *mb, uint16_t ethertype, uint16_t vf_id);
107
108 /*
109  * manage one cell's all Ethernet frames for one DL or UL LTE subframe
110  */
111 typedef struct {
112     /* -1-this subframe is not used in current frame format
113          0-this subframe can be transmitted, i.e., data is ready
114           1-this subframe is waiting transmission, i.e., data is not ready
115          10 - DL transmission missing deadline. When FE needs this subframe data but bValid is still 1,
116         set bValid to 10.
117     */
118     int32_t bValid ; // when UL rx, it is subframe index.
119     int32_t nSegToBeGen;
120     int32_t nSegGenerated; // how many date segment are generated by DL LTE processing or received from FE
121                        // -1 means that DL packet to be transmitted is not ready in BS
122     int32_t nSegTransferred; // number of data segments has been transmitted or received
123     struct rte_mbuf *pData[XRAN_N_MAX_BUFFER_SEGMENT]; // point to DPDK allocated memory pool
124     struct xran_buffer_list sBufferList;
125 } BbuIoBufCtrlStruct;
126
127
128 #define XranIncrementJob(i)                  ((i >= (XRAN_SYM_JOB_SIZE-1)) ? 0 : (i+1))
129
130 #define XRAN_MAX_PKT_BURST_PER_SYM 96 /**< 16 layers with 6 sections each */
131 #define XRAN_MAX_PACKET_FRAG 9
132
133 #define MBUF_TABLE_SIZE  (2 * MAX(XRAN_MAX_PKT_BURST_PER_SYM, XRAN_MAX_PACKET_FRAG))
134
135 #define XRAN_IQ_FLOW_MAX 512 /**< Maximum flow IQ flows per XRAN port */
136
137 #define XRAN_MAX_MEM_IF_RING_SIZE 8*32
138
139 struct mbuf_table {
140         uint16_t len;
141         struct rte_mbuf *m_table[MBUF_TABLE_SIZE];
142 };
143
144 /** Symbols CB structure defining context of exection */
145 struct cb_user_per_sym_ctx {
146     int32_t status;       /** status of CB - free, used */
147     int32_t symb_num_req; /**< requested Symb for CB */
148     int32_t sym_diff; /**< delay/advace as measured against OTA "-" - delay(later OTA) "+" - advace (earlier OTA) */
149     int32_t symb_num_ota; /**< coresponding "execution time" for Symb according to type of CB */
150     int32_t cb_type_id;   /**< type of CB */
151
152     /** DPDK timer specific variables */
153     int32_t user_timer_put;  /**< put index (producer)*/
154     int32_t user_timer_get;  /**< get index  (consumer)*/
155     struct xran_timer_ctx user_cb_timer_ctx[MAX_CB_TIMER_CTX]; /**< DPDK timer context */
156
157     xran_callback_sym_fn       symCb;        /**< call back for Symb event */
158     void                      *symCbParam;  /**< parameters of call back function */
159     struct xran_sense_of_time *symCbTimeInfo;  /**< Time related infomation to this CB */
160     void  *p_dev;       /**< poiter back to coresponding Device context */
161 };
162
163 /** Shared data at the end of an external buffer for C-plane and U-plane*/
164 struct xran_shared_data_ucp_t {
165     struct rte_mbuf_ext_shared_info sh_data[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_MAX_SECTIONS_PER_SLOT];
166 };
167
168 /** Shared data at the end of an external buffer for Beam forming weights */
169 struct xran_shared_data_bfw_t {
170     struct rte_mbuf_ext_shared_info sh_data[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_MAX_SECTIONS_PER_SLOT];
171 };
172
173 /** Shared data at the end of an external buffer for SRS */
174 struct xran_shared_data_srs_t {
175     struct rte_mbuf_ext_shared_info sh_data[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANT_ARRAY_ELM_NR];
176 };
177
178
179 /** Structure to hold the information for tracking the prb element processing across symbols.
180  *  C-Plane processing for every slot is spread equally across symbols that fall within allowed window.
181  *  This structure is used to keep track of processing that is done.
182  */
183 struct xran_prb_elm_proc_info_t {
184     uint16_t  nPrbElmPerSym;    /**< Number of prb elements to be processed per symbol */
185     uint16_t  nPrbElmProcessed; /**< Holds the number of PrbElms Processed in a given symbol time by xran. */
186     uint8_t   numSymsRemaining; /**< Number of symbols for DL CP transmission remaining in this slot */
187 };
188
189 struct __rte_cache_aligned xran_device_ctx
190 {
191     uint8_t sector_id;
192     uint8_t xran_port_id;
193     struct xran_eaxcid_config    eAxc_id_cfg;
194     struct xran_fh_init          fh_init;
195     struct xran_fh_config        fh_cfg;
196     struct xran_prach_cp_config  PrachCPConfig;
197     struct xran_prach_cp_config  PrachCPConfigLTE;
198
199     uint32_t enablePrach;
200     uint32_t enableCP;
201
202     int32_t DynamicSectionEna;
203     int32_t RunSlotPrbMapBySymbolEnable;
204     int64_t offset_sec;
205     int64_t offset_nsec;    //offset to GPS time calcuated based on alpha and beta
206     uint32_t interval_us_local;
207
208     uint32_t enableSrs;
209     uint16_t enableSrsCp;
210     uint16_t nSrsDelaySym;
211     uint8_t puschMaskEnable;
212     uint8_t puschMaskSlot;
213     struct xran_srs_config srs_cfg; /** configuration of SRS */
214
215     BbuIoBufCtrlStruct sFrontHaulTxBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
216     BbuIoBufCtrlStruct sFrontHaulTxPrbMapBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
217     BbuIoBufCtrlStruct sFrontHaulRxBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
218     BbuIoBufCtrlStruct sFrontHaulRxPrbMapBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
219     BbuIoBufCtrlStruct sFHPrachRxBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
220     BbuIoBufCtrlStruct sFHPrachRxBbuIoBufCtrlDecomp[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
221     
222     BbuIoBufCtrlStruct sFHSrsRxBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANT_ARRAY_ELM_NR];
223     BbuIoBufCtrlStruct sFHSrsRxPrbMapBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANT_ARRAY_ELM_NR];
224
225     BbuIoBufCtrlStruct sFHCpRxPrbMapBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
226     BbuIoBufCtrlStruct sFHCpTxPrbMapBbuIoBufCtrl[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
227
228     /* buffers lists */
229     struct xran_flat_buffer sFrontHaulTxBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
230     struct xran_flat_buffer sFrontHaulTxPrbMapBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
231     struct xran_flat_buffer sFrontHaulRxBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
232     struct xran_flat_buffer sFrontHaulRxPrbMapBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
233     struct xran_flat_buffer sFHPrachRxBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
234     struct xran_flat_buffer sFHPrachRxBuffersDecomp[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
235     
236     struct xran_flat_buffer sFHSrsRxBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANT_ARRAY_ELM_NR][XRAN_MAX_NUM_OF_SRS_SYMBOL_PER_SLOT];
237     struct xran_flat_buffer sFHSrsRxPrbMapBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANT_ARRAY_ELM_NR];
238
239     // struct xran_flat_buffer sFHCpRxPrbMapBuffers[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
240
241     xran_transport_callback_fn pCallback[XRAN_MAX_SECTOR_NR];
242     void *pCallbackTag[XRAN_MAX_SECTOR_NR];
243
244     xran_transport_callback_fn pPrachCallback[XRAN_MAX_SECTOR_NR];
245     void *pPrachCallbackTag[XRAN_MAX_SECTOR_NR];
246
247     xran_transport_callback_fn pSrsCallback[XRAN_MAX_SECTOR_NR];
248     void *pSrsCallbackTag[XRAN_MAX_SECTOR_NR];
249
250     LIST_HEAD(sym_cb_elem_list, cb_elem_entry) sym_cb_list_head[XRAN_NUM_OF_SYMBOL_PER_SLOT];
251
252     uint8_t numSetBFWs_arr[XRAN_MAX_SECTIONS_PER_SLOT];
253
254     int32_t sym_up; /**< when we start sym 0 of up with respect to OTA time as measured in symbols */
255     int32_t sym_up_ul;
256
257     xran_fh_tti_callback_fn ttiCb[XRAN_CB_MAX];
258     void *TtiCbParam[XRAN_CB_MAX];
259     uint32_t SkipTti[XRAN_CB_MAX];
260
261     int xran2phy_mem_ready;
262
263     int rx_packet_symb_tracker[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
264     int rx_packet_prach_tracker[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT];
265     int rx_packet_callback_tracker[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR];
266     int rx_packet_prach_callback_tracker[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR];
267     int prach_start_symbol[XRAN_MAX_SECTOR_NR];
268     int prach_last_symbol[XRAN_MAX_SECTOR_NR];
269
270     int phy_tti_cb_done;
271
272     struct rte_mempool *direct_pool;
273     struct rte_mempool *indirect_pool;
274
275     struct xran_common_counters fh_counters;
276
277     xran_ethdi_mbuf_send_fn send_cpmbuf2ring;   /**< callback to send mbufs of C-Plane packets to the VF ring */
278     xran_ethdi_mbuf_send_fn send_upmbuf2ring;   /**< callback to send mbufs of U-Plane packets to the VF ring */
279
280     struct xran_timer_ctx timer_ctx[MAX_NUM_OF_XRAN_CTX];
281     struct xran_timer_ctx cb_timer_ctx[MAX_CB_TIMER_CTX];
282
283     struct rte_timer tti_to_phy_timer[MAX_TTI_TO_PHY_TIMER];
284     struct rte_timer sym_timer;
285     struct rte_timer dpdk_timer[MAX_NUM_OF_DPDK_TIMERS];
286
287     uint16_t map2vf[2][XRAN_COMPONENT_CARRIERS_MAX][XRAN_MAX_ANTENNA_NR*2 + XRAN_MAX_ANT_ARRAY_ELM_NR][XRAN_VF_MAX];
288
289     int32_t ctx;
290
291     int timer_put;
292
293     struct cb_user_per_sym_ctx symCbCtx[XRAN_NUM_OF_SYMBOL_PER_SLOT][XRAN_CB_SYM_MAX];
294
295     volatile int32_t timing_source_thread_running;
296
297     struct rte_mbuf *to_free_mbuf[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR][XRAN_NUM_OF_SYMBOL_PER_SLOT][XRAN_MAX_SECTIONS_PER_SLOT];
298
299     tx_sym_gen_fn tx_sym_gen_func;
300
301     int32_t job2wrk_id[XRAN_JOB_TYPE_MAX]; /** mapping of HI prio Job to worker core */
302
303     struct xran_shared_data_ucp_t share_data;
304     struct xran_shared_data_ucp_t cp_share_data;
305     struct xran_shared_data_bfw_t bfw_share_data;
306     struct xran_shared_data_srs_t srs_share_data;
307
308     struct rte_flow *p_iq_flow[XRAN_IQ_FLOW_MAX];
309     uint32_t iq_flow_cnt;  /**< number of IQ flows configured */
310
311     uint8_t ndm_srs_scheduled;      /* set if SRS has been scheduled */
312     uint8_t ndm_srs_schedperiod;    /* SRS slot within TDD period */
313     uint32_t ndm_srs_txtti;         /* first slot for transmit SRS within TDD period */
314     uint32_t ndm_srs_tti;           /* original SRS slot */
315     uint8_t numSymsForDlCP; /**< number of symbols for DL CP transmission */
316     struct xran_prb_elm_proc_info_t prbElmProcInfo[XRAN_N_FE_BUF_LEN][XRAN_MAX_SECTOR_NR][XRAN_MAX_ANTENNA_NR];
317
318     uint8_t dssEnable;      /**< enable DSS (extension-9) */
319     uint8_t dssPeriod;      /**< DSS pattern period for LTE/NR */
320     uint8_t technology[XRAN_MAX_DSS_PERIODICITY];   /**< technology array represents slot is LTE(0)/NR(1) */
321     /* Keeps track of how many sections are processed while parsing C-plan packet */
322     uint8_t sectiondb_elm[XRAN_MAX_SECTIONDB_CTX][XRAN_DIR_MAX][XRAN_COMPONENT_CARRIERS_MAX][XRAN_MAX_ANTENNA_NR * 2 + XRAN_MAX_ANT_ARRAY_ELM_NR];
323 };
324
325 struct xran_eaxcid_config *xran_get_conf_eAxC(void *pHandle);
326 uint8_t xran_get_conf_prach_scs(void *pHandle);
327 uint8_t xran_get_conf_fftsize(void *pHandle);
328 uint8_t xran_get_conf_numerology(void *pHandle);
329 uint8_t xran_get_conf_iqwidth_prach(void *pHandle);
330 uint8_t xran_get_conf_compmethod_prach(void *pHandle);
331 uint8_t xran_get_conf_num_bfweights(void *pHandle);
332 uint8_t xran_get_num_cc(void *pHandle);
333 uint8_t xran_get_num_eAxc(void *pHandle);
334 uint8_t xran_get_num_eAxcUl(void *pHandle);
335 uint8_t xran_get_num_ant_elm(void *pHandle);
336 enum xran_category xran_get_ru_category(void *pHandle);
337 uint16_t xran_get_beamid(void *pHandle, uint8_t dir, uint8_t cc_id, uint8_t ant_id, uint8_t slot_id);
338
339 int32_t xran_dev_create_ctx(uint32_t xran_ports_num);
340 int32_t xran_dev_destroy_ctx();
341 struct xran_device_ctx *xran_dev_get_ctx(void);
342 struct xran_device_ctx *xran_dev_get_ctx_by_id(uint32_t xran_port_id);
343 struct xran_device_ctx **xran_dev_get_ctx_addr(void);
344
345 struct cb_elem_entry *xran_create_cb(XranSymCallbackFn cb_fn, void *cb_data, void* p_dev_ctx);
346 int32_t xran_destroy_cb(struct cb_elem_entry * cb_elm);
347
348 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);
349 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);
350
351 uint16_t xran_set_map_ecpriRtcid_to_vf(struct xran_device_ctx *p_dev_ctx, int32_t dir, int32_t cc_id, int32_t ru_port_id, uint16_t vf_id);
352 uint16_t xran_set_map_ecpriPcid_to_vf(struct xran_device_ctx *p_dev_ctx,  int32_t dir, int32_t cc_id, int32_t ru_port_id, uint16_t vf_id);
353
354 int32_t xran_init_vfs_mapping(void *pHandle);
355 int32_t xran_init_vf_rxq_to_pcid_mapping(void *pHandle);
356
357 static inline int8_t xran_dev_ctx_get_port_id(void * handle)
358 {
359     struct xran_device_ctx * p_dev_ctx  = (struct xran_device_ctx *)handle;
360     if(p_dev_ctx)
361         return (int8_t)p_dev_ctx->xran_port_id;
362     else
363         return -1;
364 };
365
366 #ifdef __cplusplus
367 }
368 #endif
369
370 #endif
371