9dd4781a7c56668988eb5b1f3625dde613f29c45
[o-du/phy.git] / fhi_lib / lib / src / xran_frame_struct.c
1 /******************************************************************************
2 *
3 *   Copyright (c) 2019 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 /**
21  * @brief XRAN layer common functionality for both lls-CU and RU as well as C-plane and
22  *    U-plane
23  * @file xran_common.c
24  * @ingroup group_source_xran
25  * @author Intel Corporation
26  **/
27
28 #include <assert.h>
29 #include <err.h>
30 #include <arpa/inet.h>
31 #include <sys/time.h>
32 #include <time.h>
33
34 #include "xran_frame_struct.h"
35 #include "xran_printf.h"
36
37 enum nXranChBwOptions
38 {
39     XRAN_BW_5_0_MHZ  = 5,  XRAN_BW_10_0_MHZ = 10, XRAN_BW_15_0_MHZ = 15, XRAN_BW_20_0_MHZ = 20, XRAN_BW_25_0_MHZ = 25,
40     XRAN_BW_30_0_MHZ = 30, XRAN_BW_40_0_MHZ = 40, XRAN_BW_50_0_MHZ = 50, XRAN_BW_60_0_MHZ = 60, XRAN_BW_70_0_MHZ = 70,
41     XRAN_BW_80_0_MHZ = 80, XRAN_BW_90_0_MHZ = 90, XRAN_BW_100_0_MHZ = 100, XRAN_BW_200_0_MHZ = 200, XRAN_BW_400_0_MHZ = 400
42 };
43
44 // F1 Tables 38.101-1 Table 5.3.2-1. Maximum transmission bandwidth configuration NRB
45 static uint16_t nNumRbsPerSymF1[3][13] =
46 {
47     //  5MHz    10MHz   15MHz   20 MHz  25 MHz  30 MHz  40 MHz  50MHz   60 MHz  70 MHz  80 MHz   90 MHz  100 MHz
48         {25,    52,     79,     106,    133,    160,    216,    270,    0,         0,      0,      0,      0},         // Numerology 0 (15KHz)
49         {11,    24,     38,     51,     65,     78,     106,    133,    162,       0,    217,    245,    273},         // Numerology 1 (30KHz)
50         {0,     11,     18,     24,     31,     38,     51,     65,     79,        0,    107,    121,    135}          // Numerology 2 (60KHz)
51 };
52
53 // F2 Tables 38.101-2 Table 5.3.2-1. Maximum transmission bandwidth configuration NRB
54 static uint16_t nNumRbsPerSymF2[2][4] =
55 {
56     //  50Mhz  100MHz  200MHz   400MHz
57         {66,    132,    264,     0},        // Numerology 2 (60KHz)
58         {32,    66,     132,     264}       // Numerology 3 (120KHz)
59 };
60
61 // 38.211 - Table 4.2.1
62 static uint16_t nSubCarrierSpacing[5] =
63 {
64     15,     // mu = 0
65     30,     // mu = 1
66     60,     // mu = 2
67     120,    // mu = 3
68     240     // mu = 4
69 };
70
71 // TTI interval in us (slot duration)
72 static uint16_t nTtiInterval[4] =
73 {
74     1000,    // mu = 0
75     500,     // mu = 1
76     250,     // mu = 2
77     125,     // mu = 3
78 };
79
80 // F1 Tables 38.101-1 Table F.5.3. Window length for normal CP
81 static uint16_t nCpSizeF1[3][13][2] =
82 {
83     //    5MHz      10MHz      15MHz       20 MHz      25 MHz     30 MHz      40 MHz       50MHz       60 MHz      70 MHz     80 MHz     90 MHz     100 MHz
84         {{40, 36}, {80, 72}, {120, 108}, {160, 144}, {160, 144}, {240, 216}, {320, 288}, {320, 288},     {0, 0},     {0, 0},     {0, 0},     {0, 0},     {0, 0}},        // Numerology 0 (15KHz)
85         {{22, 18}, {44, 36},   {66, 54},   {88, 72},   {88, 72}, {132, 108}, {176, 144}, {176, 144}, {264, 216}, {264, 216}, {352, 288}, {352, 288}, {352, 288}},       // Numerology 1 (30KHz)
86         {  {0, 0}, {26, 18},   {39, 27},   {52, 36},   {52, 36},   {78, 54},  {104, 72},  {104, 72}, {156, 108}, {156, 108}, {208, 144}, {208, 144}, {208, 144}},       // Numerology 2 (60KHz)
87 };
88
89 // F2 Tables 38.101-2 Table F.5.3. Window length for normal CP
90 static int16_t nCpSizeF2[2][4][2] =
91 {
92     //    50Mhz    100MHz      200MHz     400MHz
93         {  {0, 0}, {104, 72}, {208, 144}, {416, 288}}, // Numerology 2 (60KHz)
94         {{68, 36}, {136, 72}, {272, 144}, {544, 288}}, // Numerology 3 (120KHz)
95 };
96
97 static uint32_t xran_fs_max_slot_num = 8000;
98 static uint16_t xran_fs_num_slot_tdd_loop[XRAN_MAX_SECTOR_NR] = { XRAN_NUM_OF_SLOT_IN_TDD_LOOP };
99 static uint16_t xran_fs_num_dl_sym_sp[XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SLOT_IN_TDD_LOOP] = {0};
100 static uint16_t xran_fs_num_ul_sym_sp[XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SLOT_IN_TDD_LOOP] = {0};
101 static uint8_t xran_fs_slot_type[XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SLOT_IN_TDD_LOOP] = {{XRAN_SLOT_TYPE_INVALID}};
102 static uint8_t xran_fs_slot_symb_type[XRAN_MAX_SECTOR_NR][XRAN_NUM_OF_SLOT_IN_TDD_LOOP][XRAN_NUM_OF_SYMBOL_PER_SLOT] = {{{XRAN_SLOT_TYPE_INVALID}}};
103 static float xran_fs_ul_rate[XRAN_MAX_SECTOR_NR] = {0.0};
104 static float xran_fs_dl_rate[XRAN_MAX_SECTOR_NR] = {0.0};
105
106 uint32_t xran_fs_get_tti_interval(uint8_t nMu)
107 {
108     if (nMu < 4)
109     {
110         return nTtiInterval[nMu];
111     }
112     else
113     {
114         printf("ERROR: %s Mu[%d] is not valid, setting to 0\n",__FUNCTION__, nMu);
115         return nTtiInterval[0];
116     }
117 }
118
119 uint32_t xran_fs_get_scs(uint8_t nMu)
120 {
121     if (nMu <= 3)
122     {
123         return nSubCarrierSpacing[nMu];
124     }
125     else
126     {
127         printf("ERROR: %s Mu[%d] is not valid\n",__FUNCTION__, nMu);
128     }
129
130     return 0;
131 }
132
133 //-------------------------------------------------------------------------------------------
134 /** @ingroup group_nr5g_source_phy_common
135  *
136  *  @param[in]   nNumerology - Numerology determine sub carrier spacing, Value: 0->4 0: 15khz,  1: 30khz,  2: 60khz 3: 120khz, 4: 240khz
137  *  @param[in]   nBandwidth - Carrier bandwidth for in MHz. Value: 5->400
138  *  @param[in]   nAbsFrePointA - Abs Freq Point A of the Carrier Center Frequency for in KHz Value: 450000->52600000
139  *
140  *  @return  Number of RBs in cell
141  *
142  *  @description
143  *  Returns number of RBs based on 38.101-1 and 38.101-2 for the cell
144  *
145 **/
146 //-------------------------------------------------------------------------------------------
147 uint16_t xran_fs_get_num_rbs(uint32_t nNumerology, uint32_t nBandwidth, uint32_t nAbsFrePointA)
148 {
149     uint32_t error = 1;
150     uint16_t numRBs = 0;
151
152     if (nAbsFrePointA <= 6000000)
153     {
154         // F1 Tables 38.101-1 Table 5.3.2-1. Maximum transmission bandwidth configuration NRB
155         if (nNumerology < 3)
156         {
157             switch(nBandwidth)
158             {
159                 case XRAN_BW_5_0_MHZ:
160                     numRBs = nNumRbsPerSymF1[nNumerology][0];
161                     error = 0;
162                 break;
163                 case XRAN_BW_10_0_MHZ:
164                     numRBs = nNumRbsPerSymF1[nNumerology][1];
165                     error = 0;
166                 break;
167                 case XRAN_BW_15_0_MHZ:
168                     numRBs = nNumRbsPerSymF1[nNumerology][2];
169                     error = 0;
170                 break;
171                 case XRAN_BW_20_0_MHZ:
172                     numRBs = nNumRbsPerSymF1[nNumerology][3];
173                     error = 0;
174                 break;
175                 case XRAN_BW_25_0_MHZ:
176                     numRBs = nNumRbsPerSymF1[nNumerology][4];
177                     error = 0;
178                 break;
179                 case XRAN_BW_30_0_MHZ:
180                     numRBs = nNumRbsPerSymF1[nNumerology][5];
181                     error = 0;
182                 break;
183                 case XRAN_BW_40_0_MHZ:
184                     numRBs = nNumRbsPerSymF1[nNumerology][6];
185                     error = 0;
186                 break;
187                 case XRAN_BW_50_0_MHZ:
188                     numRBs = nNumRbsPerSymF1[nNumerology][7];
189                     error = 0;
190                 break;
191                 case XRAN_BW_60_0_MHZ:
192                     numRBs = nNumRbsPerSymF1[nNumerology][8];
193                     error = 0;
194                 break;
195                 case XRAN_BW_70_0_MHZ:
196                     numRBs = nNumRbsPerSymF1[nNumerology][9];
197                     error = 0;
198                 break;
199                 case XRAN_BW_80_0_MHZ:
200                     numRBs = nNumRbsPerSymF1[nNumerology][10];
201                     error = 0;
202                 break;
203                 case XRAN_BW_90_0_MHZ:
204                     numRBs = nNumRbsPerSymF1[nNumerology][11];
205                     error = 0;
206                 break;
207                 case XRAN_BW_100_0_MHZ:
208                     numRBs = nNumRbsPerSymF1[nNumerology][12];
209                     error = 0;
210                 break;
211                 default:
212                     error = 1;
213                 break;
214             }
215         }
216     }
217     else
218     {
219         if ((nNumerology >= 2) && (nNumerology <= 3))
220         {
221             // F2 Tables 38.101-2 Table 5.3.2-1. Maximum transmission bandwidth configuration NRB
222             switch(nBandwidth)
223             {
224                 case XRAN_BW_50_0_MHZ:
225                     numRBs = nNumRbsPerSymF2[nNumerology-2][0];
226                     error = 0;
227                 break;
228                 case XRAN_BW_100_0_MHZ:
229                     numRBs = nNumRbsPerSymF2[nNumerology-2][1];
230                     error = 0;
231                 break;
232                 case XRAN_BW_200_0_MHZ:
233                     numRBs = nNumRbsPerSymF2[nNumerology-2][2];
234                     error = 0;
235                 break;
236                 case XRAN_BW_400_0_MHZ:
237                     numRBs = nNumRbsPerSymF2[nNumerology-2][3];
238                     error = 0;
239                 break;
240                 default:
241                     error = 1;
242                 break;
243             }
244         }
245     }
246
247
248     if (error)
249     {
250         printf("ERROR: %s: nNumerology[%d] nBandwidth[%d] nAbsFrePointA[%d]\n",__FUNCTION__, nNumerology, nBandwidth, nAbsFrePointA);
251     }
252     else
253     {
254         printf("%s: nNumerology[%d] nBandwidth[%d] nAbsFrePointA[%d] numRBs[%d]\n",__FUNCTION__, nNumerology, nBandwidth, nAbsFrePointA, numRBs);
255     }
256
257     return numRBs;
258 }
259
260 //-------------------------------------------------------------------------------------------
261 /** @ingroup phy_cal_nrarfcn
262  *
263  *  @param[in]   center frequency
264  *
265  *  @return  NR-ARFCN
266  *
267  *  @description
268  *  This calculates NR-ARFCN value according to center frequency
269  *
270 **/
271 //-------------------------------------------------------------------------------------------
272 uint32_t xran_fs_cal_nrarfcn(uint32_t nCenterFreq)
273 {
274     uint32_t nDeltaFglobal,nFoffs,nNoffs;
275     uint32_t nNRARFCN = 0;
276
277     if(nCenterFreq > 0 && nCenterFreq < 3000*1000)
278     {
279         nDeltaFglobal = 5;
280         nFoffs = 0;
281         nNoffs = 0;
282     }
283     else if(nCenterFreq >= 3000*1000 && nCenterFreq < 24250*1000)
284     {
285         nDeltaFglobal = 15;
286         nFoffs = 3000*1000;
287         nNoffs = 600000;
288     }
289     else if(nCenterFreq >= 24250*1000 && nCenterFreq <= 100000*1000)
290     {
291         nDeltaFglobal = 60;
292         nFoffs = 24250080;
293         nNoffs = 2016667;
294     }
295     else
296     {
297          printf("@@@@ incorrect center frerquency %d\n",nCenterFreq);
298          return (0);
299     }
300
301     nNRARFCN = ((nCenterFreq - nFoffs)/nDeltaFglobal) + nNoffs;
302
303     printf("%s: nCenterFreq[%d] nDeltaFglobal[%d] nFoffs[%d] nNoffs[%d] nNRARFCN[%d]\n", __FUNCTION__, nCenterFreq, nDeltaFglobal, nFoffs, nNoffs, nNRARFCN);
304     return (nNRARFCN);
305 }
306
307 uint32_t  xran_fs_slot_limit_init(int32_t tti_interval_us)
308 {
309     xran_fs_max_slot_num = (1000/tti_interval_us)*1000;
310     return xran_fs_max_slot_num;
311 }
312
313 uint32_t xran_fs_get_max_slot(void)
314 {
315     return xran_fs_max_slot_num;
316 }
317
318 int32_t xran_fs_slot_limit(int32_t nSfIdx)
319 {
320     while (nSfIdx < 0) {
321         nSfIdx += xran_fs_max_slot_num;
322     }
323
324     while (nSfIdx >= xran_fs_max_slot_num) {
325         nSfIdx -= xran_fs_max_slot_num;
326     }
327
328     return nSfIdx;
329 }
330
331 void xran_fs_clear_slot_type(uint32_t nPhyInstanceId)
332 {
333     xran_fs_ul_rate[nPhyInstanceId] = 0.0;
334     xran_fs_dl_rate[nPhyInstanceId] = 0.0;
335     xran_fs_num_slot_tdd_loop[nPhyInstanceId] = 1;
336 }
337
338 int32_t xran_fs_set_slot_type(uint32_t nPhyInstanceId, uint32_t nFrameDuplexType, uint32_t nTddPeriod, struct xran_slot_config* psSlotConfig)
339 {
340     uint32_t nSlotNum, nSymNum, nVal, i, j;
341     uint32_t numDlSym, numUlSym, numGuardSym;
342     uint32_t numDlSlots = 0, numUlSlots = 0, numSpDlSlots = 0, numSpUlSlots = 0, numSpSlots = 0;
343     char sSlotPattern[XRAN_SLOT_TYPE_LAST][10] = {"IN\0", "DL\0", "UL\0", "SP\0", "FD\0"};
344
345     // nPhyInstanceId    Carrier ID
346     // nFrameDuplexType  0 = FDD 1 = TDD
347     // nTddPeriod        Tdd Periodicity
348     // psSlotConfig[80]  Slot Config Structure for nTddPeriod Slots
349
350     xran_fs_ul_rate[nPhyInstanceId] = 0.0;
351     xran_fs_dl_rate[nPhyInstanceId] = 0.0;
352     xran_fs_num_slot_tdd_loop[nPhyInstanceId] = nTddPeriod;
353
354     for (i = 0; i < XRAN_NUM_OF_SLOT_IN_TDD_LOOP; i++)
355     {
356         xran_fs_slot_type[nPhyInstanceId][i] = XRAN_SLOT_TYPE_INVALID;
357         xran_fs_num_dl_sym_sp[nPhyInstanceId][i] = 0;
358         xran_fs_num_ul_sym_sp[nPhyInstanceId][i] = 0;
359     }
360
361     if (nFrameDuplexType == XRAN_FDD)
362     {
363         for (i = 0; i < XRAN_NUM_OF_SLOT_IN_TDD_LOOP; i++)
364         {
365             xran_fs_slot_type[nPhyInstanceId][i] = XRAN_SLOT_TYPE_FDD;
366             for(j = 0; j < XRAN_NUM_OF_SYMBOL_PER_SLOT; j++)
367               xran_fs_slot_symb_type[nPhyInstanceId][i][j] = XRAN_SYMBOL_TYPE_FDD;
368         }
369         xran_fs_num_slot_tdd_loop[nPhyInstanceId] = 1;
370         xran_fs_dl_rate[nPhyInstanceId] = 1.0;
371         xran_fs_ul_rate[nPhyInstanceId] = 1.0;
372     }
373     else
374     {
375         for (nSlotNum = 0; nSlotNum < nTddPeriod; nSlotNum++)
376         {
377             numDlSym = 0;
378             numUlSym = 0;
379             numGuardSym = 0;
380             for (nSymNum = 0; nSymNum < XRAN_NUM_OF_SYMBOL_PER_SLOT; nSymNum++)
381             {
382                 switch(psSlotConfig[nSlotNum].nSymbolType[nSymNum])
383                 {
384                     case XRAN_SYMBOL_TYPE_DL:
385                         numDlSym++;
386                         xran_fs_slot_symb_type[nPhyInstanceId][nSlotNum][nSymNum] = XRAN_SYMBOL_TYPE_DL;
387                     break;
388                     case XRAN_SYMBOL_TYPE_GUARD:
389                         xran_fs_slot_symb_type[nPhyInstanceId][nSlotNum][nSymNum] = XRAN_SYMBOL_TYPE_GUARD;
390                         numGuardSym++;
391                     break;
392                     default:
393                         xran_fs_slot_symb_type[nPhyInstanceId][nSlotNum][nSymNum] = XRAN_SYMBOL_TYPE_UL;
394                         numUlSym++;
395                     break;
396                 }
397             }
398
399             print_dbg("nSlotNum[%d] : numDlSym[%d] numGuardSym[%d] numUlSym[%d] ", nSlotNum, numDlSym, numGuardSym, numUlSym);
400
401             if ((numUlSym == 0) && (numGuardSym == 0))
402             {
403                 xran_fs_slot_type[nPhyInstanceId][nSlotNum] = XRAN_SLOT_TYPE_DL;
404                 numDlSlots++;
405                 print_dbg("XRAN_SLOT_TYPE_DL\n");
406             }
407             else if ((numDlSym == 0) && (numGuardSym == 0))
408             {
409                 xran_fs_slot_type[nPhyInstanceId][nSlotNum] = XRAN_SLOT_TYPE_UL;
410                 numUlSlots++;
411                 print_dbg("XRAN_SLOT_TYPE_UL\n");
412             }
413             else
414             {
415                 xran_fs_slot_type[nPhyInstanceId][nSlotNum] = XRAN_SLOT_TYPE_SP;
416                 numSpSlots++;
417                 print_dbg("XRAN_SLOT_TYPE_SP\n");
418
419                 if (numDlSym)
420                 {
421                     numSpDlSlots++;
422                     xran_fs_num_dl_sym_sp[nPhyInstanceId][nSlotNum] = numDlSym;
423                 }
424                 if (numUlSym)
425                 {
426                     numSpUlSlots++;
427                     xran_fs_num_ul_sym_sp[nPhyInstanceId][nSlotNum] = numUlSym;
428                 }
429             }
430             print_dbg("            numDlSlots[%d] numUlSlots[%d] numSpSlots[%d] numSpDlSlots[%d] numSpUlSlots[%d]\n", numDlSlots, numUlSlots, numSpSlots, numSpDlSlots, numSpUlSlots);
431         }
432
433         xran_fs_dl_rate[nPhyInstanceId] = (float)(numDlSlots + numSpDlSlots) / (float)nTddPeriod;
434         xran_fs_ul_rate[nPhyInstanceId] = (float)(numUlSlots + numSpUlSlots) / (float)nTddPeriod;
435     }
436
437     print_dbg("%s: nPhyInstanceId[%d] nFrameDuplexType[%d], nTddPeriod[%d]\n",
438         __FUNCTION__, nPhyInstanceId, nFrameDuplexType, nTddPeriod);
439
440     print_dbg("DLRate[%f] ULRate[%f]\n", xran_fs_dl_rate[nPhyInstanceId], xran_fs_ul_rate[nPhyInstanceId]);
441
442     nVal = (xran_fs_num_slot_tdd_loop[nPhyInstanceId] < 10) ? xran_fs_num_slot_tdd_loop[nPhyInstanceId] : 10;
443
444     print_dbg("SlotPattern:\n");
445     print_dbg("Slot:   ");
446     for (nSlotNum = 0; nSlotNum < nVal; nSlotNum++)
447     {
448         print_dbg("%d    ", nSlotNum);
449     }
450     print_dbg("\n");
451
452     print_dbg("  %3d   ", 0);
453     for (nSlotNum = 0, i = 0; nSlotNum < xran_fs_num_slot_tdd_loop[nPhyInstanceId]; nSlotNum++)
454     {
455         print_dbg("%s   ", sSlotPattern[xran_fs_slot_type[nPhyInstanceId][nSlotNum]]);
456         i++;
457         if ((i == 10) && ((nSlotNum+1) < xran_fs_num_slot_tdd_loop[nPhyInstanceId]))
458         {
459             print_dbg("\n");
460             print_dbg("  %3d   ", nSlotNum);
461             i = 0;
462         }
463     }
464     print_dbg("\n\n");
465
466     return 0;
467 }
468
469 int32_t xran_fs_get_slot_type(int32_t nCellIdx, int32_t nSlotdx, int32_t nType)
470 {
471     int32_t nSfIdxMod, nSfType, ret = 0;
472
473     nSfIdxMod = xran_fs_slot_limit(nSlotdx) % ((xran_fs_num_slot_tdd_loop[nCellIdx] > 0) ? xran_fs_num_slot_tdd_loop[nCellIdx]: 1);
474     nSfType = xran_fs_slot_type[nCellIdx][nSfIdxMod];
475
476     if (nSfType == nType)
477     {
478         ret = 1;
479     }
480     else if (nSfType == XRAN_SLOT_TYPE_SP)
481     {
482         if ((nType == XRAN_SLOT_TYPE_DL) && xran_fs_num_dl_sym_sp[nCellIdx][nSfIdxMod])
483         {
484             ret = 1;
485         }
486
487         if ((nType == XRAN_SLOT_TYPE_UL) && xran_fs_num_ul_sym_sp[nCellIdx][nSfIdxMod])
488         {
489             ret = 1;
490         }
491     }
492     else if (nSfType == XRAN_SLOT_TYPE_FDD)
493     {
494         ret = 1;
495     }
496
497     return ret;
498 }
499
500 int32_t xran_fs_get_symbol_type(int32_t nCellIdx, int32_t nSlotdx,  int32_t nSymbIdx)
501 {
502     int32_t nSfIdxMod, nSfType, ret = 0;
503
504     nSfIdxMod = xran_fs_slot_limit(nSlotdx) % ((xran_fs_num_slot_tdd_loop[nCellIdx] > 0) ? xran_fs_num_slot_tdd_loop[nCellIdx]: 1);
505
506     return xran_fs_slot_symb_type[nCellIdx][nSfIdxMod][nSymbIdx];
507 }
508
509