e1b961991061e7c578202daac9bfab981045d713
[o-du/l2.git] / src / 5gnrsch / sch_rach.c
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
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      Name:     sch_rach.c
22   
23      Type:     C source file
24   
25      Desc:     C source code for rach handling functions
26   
27      File:     sch_rach.c
28   
29 **********************************************************************/
30
31 /** @file sch_rach.c
32 @brief This file implements the rach handling.
33 */
34 #include "common_def.h"
35 #include "tfu.h"
36 #include "lrg.h"
37 #include "tfu.x"
38 #include "lrg.x"
39 #include "du_log.h"
40 #include "du_app_mac_inf.h"
41 #include "mac_sch_interface.h"
42 #include "sch.h"
43 #include "sch_utils.h"
44
45 /**
46  * @brief Checks if PRACH can be scheduled in current slot
47  *
48  * @details
49  *
50  *     Function : schCheckPrachOcc
51  *
52  *     This function checks if PRACH can be scheduled in 
53  *     current slot
54  *
55  *  @param[in]  Cell Cb
56  *              Slot timing
57  *  @return  TRUE
58  *           FALSE
59  **/
60 bool schCheckPrachOcc(SchCellCb *cell, SlotTimingInfo prachOccasionTimingInfo)
61 {
62    uint8_t  prachCfgIdx = 0;
63    uint8_t  x = 0;
64    uint8_t  y = 0;
65    uint8_t  subFrame = 0;
66    uint16_t prachSubframe = 0;
67
68    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
69
70    /* derive the prachCfgIdx table paramters */
71    x                = prachCfgIdxTable[prachCfgIdx][1];
72    y                = prachCfgIdxTable[prachCfgIdx][2];
73    prachSubframe    = prachCfgIdxTable[prachCfgIdx][3];
74
75    if((prachOccasionTimingInfo.sfn%x) == y)
76    {
77       subFrame = prachOccasionTimingInfo.slot/pow(2, cell->cellCfg.numerology);
78
79       /* check for subFrame number */
80       if ((1 << subFrame) & prachSubframe)
81       {
82          /* prach ocassion present in this subframe */
83 #ifdef NR_TDD
84          if(UL_SLOT != schGetSlotSymbFrmt(prachOccasionTimingInfo.slot % cell->numSlotsInPeriodicity,\
85          cell->slotFrmtBitMap))
86          {
87             DU_LOG("\nERROR  --> SCH : PrachCfgIdx %d doesn't support UL slot", prachCfgIdx);
88             return FALSE;
89          }
90 #endif
91          return TRUE;
92       }
93    }
94    return FALSE;
95 }
96
97 /**
98  * @brief Calculate number of PRBs to be allocated for PRACH 
99  *
100  * @details
101  *
102  *     Function : schCalcPrachNumRb
103  *
104  *     Calculate number of PRBs to be allocated for PRACH
105  *
106  *  @param[in]  SchCellCb *cell, cell cb
107  *  @return  Number of PRBs
108  **/
109 uint8_t schCalcPrachNumRb(SchCellCb *cell)
110 {
111    uint8_t tableIdx = 0;
112    uint16_t puschScs = fetchScsValue(cell->cellCfg.schInitialUlBwp.bwp.scs);
113
114    for(tableIdx=0; tableIdx < MAX_RACH_NUM_RB_IDX; tableIdx++)
115    {
116       if((numRbForPrachTable[tableIdx][0] == cell->cellCfg.schRachCfg.rootSeqLen) &&
117             (numRbForPrachTable[tableIdx][1] == cell->cellCfg.schRachCfg.prachSubcSpacing) &&
118             (numRbForPrachTable[tableIdx][2] == puschScs))
119       {
120          return numRbForPrachTable[tableIdx][3];
121       }
122    }
123    return 0;
124 }
125
126 /**
127  * @brief resource allocation for PRACH
128  *
129  * @details
130  *
131  *     Function : schPrachResAlloc
132  *
133  *     This function handles PRACH allocation
134  *
135  *  @param[in]  SchCellCb *cell, cell cb
136  *  @param[in]  UlSchedInfo *ulSchedInfo, UL scheduling info
137  *  @return  void
138  **/
139 void schPrachResAlloc(SchCellCb *cell, UlSchedInfo *ulSchedInfo, SlotTimingInfo prachOccasionTimingInfo)
140 {
141    uint8_t  numPrachRb = 0;
142    uint8_t  numRa = 0;
143    uint8_t  prachCfgIdx = 0;
144    uint8_t  prachFormat = 0;
145    uint8_t  prachStartSymbol = 0;
146    uint8_t  prachDuration = 0;
147    uint8_t  prachOcas = 0;
148    uint8_t  dataType = 0;
149    uint16_t freqStart = 0;
150
151    /* If this slot is not a PRACH occassion, return */
152    if(!schCheckPrachOcc(cell, prachOccasionTimingInfo))
153       return;
154
155    prachCfgIdx      = cell->cellCfg.schRachCfg.prachCfgIdx;
156    prachFormat      = prachCfgIdxTable[prachCfgIdx][0];
157    prachStartSymbol = prachCfgIdxTable[prachCfgIdx][4];
158    prachOcas        = prachCfgIdxTable[prachCfgIdx][6];
159    prachDuration    = prachCfgIdxTable[prachCfgIdx][7];
160
161    /* numRa determined as 𝑛 belonging {0,1,.., M − 1},
162     * where M is given by msg1Fdm */
163    numRa = (cell->cellCfg.schRachCfg.msg1Fdm - 1);
164
165    /* freq domain resource determination for RACH*/
166    freqStart = cell->cellCfg.schRachCfg.msg1FreqStart;
167    numPrachRb = schCalcPrachNumRb(cell);
168    /* Allocate PRACH resources from the UL resource bitmap */
169    allocatePrbUl(cell, prachOccasionTimingInfo, prachStartSymbol, prachDuration, &freqStart, numPrachRb);
170
171    /* prach info */
172    dataType |= SCH_DATATYPE_PRACH;
173    ulSchedInfo->dataType = dataType;
174    ulSchedInfo->prachSchInfo.numPrachOcas   = prachOcas;
175    ulSchedInfo->prachSchInfo.prachFormat    = prachFormat;
176    ulSchedInfo->prachSchInfo.numRa          = numRa;
177    ulSchedInfo->prachSchInfo.prachStartSymb = prachStartSymbol;
178    DU_LOG("\nINFO  --> SCH : RACH occassion set for slot %d", prachOccasionTimingInfo.slot);
179 }
180
181 /**
182  * @brief calculate ra-rnti function. 
183  *
184  * @details
185  *
186  *     Function : calculateRaRnti
187  *     
188  *     This function calculates ra-rnti
189  *     
190  *  @param[in]  symbol index
191  *  @param[in]  slot index
192  *  @param[in]  frequency index
193  *  @return  ra-rnti
194  **/
195 uint16_t calculateRaRnti(uint8_t symbolIdx, uint8_t slotIdx, uint8_t freqIdx)
196 {
197    uint16_t raRnti = 0;
198    uint8_t  ulCarrierIdx = 0; /* Uplink carrier used for MSG1 transmission. 0:NUL carrier; 1:SUL carrier */
199    
200    /* Refer to spec 38.321, section 5.1.3 */
201    raRnti = (1 + symbolIdx + (14*slotIdx) + (14*80*freqIdx) + (14*80*8*ulCarrierIdx));
202    return raRnti;
203 }
204
205 /**
206  * @brief create raCb function. 
207  *
208  * @details
209  *
210  *     Function : createSchRaCb
211  *     
212  *     This function create raCb
213  *     
214  *  @param[in]  tcrnti
215  *  @param[in]  shed instance
216  *  @return  void
217  **/
218 void createSchRaCb(uint16_t tcrnti, Inst schInst)
219 {
220    uint8_t ueIdx = 0;
221
222    GET_UE_IDX(tcrnti, ueIdx);
223    schCb[schInst].cells[schInst]->raCb[ueIdx -1].tcrnti = tcrnti;
224 }
225
226 /**
227  * @brief resource allocation for msg3 PUSCH
228  *
229  * @details
230  *
231  *     Function : schAllocMsg3Pusch 
232  *     
233  *     This function handles msg3 PUSCH allocation
234  *     
235  *  @param[in]  Inst schInst, SCH instance
236  *  @param[in]  slot, current slot
237  *  @param[out]  msg3StartRb
238  *  @param[out]  msg3NumRb
239  *  @return  void
240  **/
241 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime)
242 {
243    SchCellCb      *cell          = NULLP;
244    SchUlSlotInfo  *schUlSlotInfo = NULLP;
245    uint8_t    mcs       = DEFAULT_MCS;
246    uint8_t    startSymb = 0;
247    uint8_t    symbLen   = 0; 
248    uint16_t   startRb   = 0;
249    uint16_t   numRb     = 0;
250    uint16_t   tbSize    = 0;
251
252    cell = schCb[schInst].cells[schInst];
253    if(cell == NULL)
254    {
255       DU_LOG("\n\nERROR  -->  SCH :  Failed to find cell in schAllocMsg3Pusch");
256       return NULLP;
257    }
258
259    /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
260    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
261    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
262
263    startRb = MAX_NUM_RB;
264    tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
265    numRb = schCalcNumPrb(tbSize, mcs, symbLen);
266    numRb++; /* allocating 1 extra RB for now */
267    allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
268
269    /* Fill PUSCH scheduling details in Slot structure */
270    schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
271    SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
272    if(!schUlSlotInfo->schPuschInfo)
273    {
274       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
275       return NULLP;
276    }
277
278    tbSize = 0;  /* since nPrb has been incremented, recalculating tbSize */
279    tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
280    tbSize = tbSize / 8 ; /*bits to byte conversion*/
281
282    schUlSlotInfo->schPuschInfo->crnti             = crnti;
283    schUlSlotInfo->schPuschInfo->harqProcId        = SCH_HARQ_PROC_ID;
284    schUlSlotInfo->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
285    schUlSlotInfo->schPuschInfo->fdAlloc.startPrb  = startRb;
286    schUlSlotInfo->schPuschInfo->fdAlloc.numPrb    = numRb;
287    schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
288    schUlSlotInfo->schPuschInfo->tdAlloc.numSymb   = symbLen;
289    schUlSlotInfo->schPuschInfo->tbInfo.qamOrder   = QPSK_MODULATION;  /* QPSK modulation */
290    schUlSlotInfo->schPuschInfo->tbInfo.mcs           = mcs;
291    schUlSlotInfo->schPuschInfo->tbInfo.mcsTable   = SCH_MCS_TABLE_QAM_64;
292    schUlSlotInfo->schPuschInfo->tbInfo.ndi        = NEW_TRANSMISSION; /* new transmission */
293    schUlSlotInfo->schPuschInfo->tbInfo.rv               = 0;
294    schUlSlotInfo->schPuschInfo->tbInfo.tbSize     = tbSize;
295    schUlSlotInfo->schPuschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
296    schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
297    schUlSlotInfo->schPuschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
298
299    return schUlSlotInfo->schPuschInfo;
300 }
301
302 /**
303  * @brief Check if a time frame is in RA Response window
304  *
305  * @details
306  *
307  *     Function : isInRaRspWindow
308  *
309  *     Check if a time frame is in RA Response window
310  *
311  *  @param[in]  RA request
312  *  @param[in]  Time frame to check
313  *  @param[in]  Total number of slot per radio frame
314  *  @return  true 
315  *  @return  false
316  **/
317 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
318 {
319    uint32_t winStartTime, winEndTime, timeToCheck;
320    
321    winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
322    winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
323    timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
324
325    /* TODO : check how to handle the wrap around scenario of MAX_SFN */
326    if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
327       return WITHIN_WINDOW;
328    else if(timeToCheck < winStartTime)
329       return WINDOW_YET_TO_START;
330       
331    return WINDOW_EXPIRED;
332 }
333
334 /**
335  * @brief Processes any pending RA request
336  *
337  * @details
338  *
339  *     Function : schProcessRaReq
340  *
341  *     This function process pending RA request
342  *
343  *  @param[in]  Current timing of the cell
344  *  @return  ROK
345  **/
346 void schProcessRaReq(SlotTimingInfo currTime, SchCellCb *cell)
347 {
348    bool      k2Found = false;
349    uint8_t   ueIdx = 0, k0TblIdx = 0, k2TblIdx = 0;
350    uint8_t   k0Index = 0, k2Index = 0;
351    uint8_t   k0 = 0, k2 = 0;
352    uint8_t   puschMu = 0;
353    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
354 #ifdef NR_TDD
355    uint8_t   totalCfgSlot = 0;
356 #endif
357    uint16_t             dciSlot = 0, rarSlot = 0;
358    SlotTimingInfo       dciTime, rarTime, msg3Time;
359    RarAlloc             *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for RAR */
360    RarAlloc             *rarSlotAlloc = NULLP;    /* Stores info for transmission of RAR PDSCH */
361    SchPuschInfo         *msg3PuschInfo = NULLP;   /* Stores MSG3 PUSCH scheduling information */
362    SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;    
363    SchK2TimingInfoTbl   *msg3K2InfoTbl=NULLP;
364    RaRspWindowStatus    windowStatus=0;
365
366    while(ueIdx < MAX_NUM_UE)
367    {
368       if(cell->raReq[ueIdx] == NULLP)
369       {
370          ueIdx++;
371          continue;
372       }
373
374 #ifdef NR_TDD
375       totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
376 #endif
377       k0K1InfoTbl = &cell->cellCfg.schInitialDlBwp.k0K1InfoTbl;
378       msg3K2InfoTbl = &cell->cellCfg.schInitialUlBwp.msg3K2InfoTbl;
379       puschMu = cell->cellCfg.numerology;
380       msg3Delta = puschDeltaTable[puschMu];
381       msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
382
383       /* Calculating time frame to send DCI for RAR */
384       ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA);
385       dciSlot = dciTime.slot;
386 #ifdef NR_TDD
387       /* Consider this slot for sending DCI, only if it is a DL slot */
388       if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
389 #endif
390       {
391          
392          /* Check if this slot is within RA response window */
393          windowStatus = isInRaRspWindow(cell->raReq[ueIdx], dciTime, cell->numSlots);
394          if(windowStatus == WITHIN_WINDOW)
395          {
396             /* For all k0 values, search for a suitable k2 value to schedule MSG3.
397              * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
398              * is found. Else no scheduling happens. 
399              */
400             for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
401             {
402                k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
403                k0 = cell->cellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
404
405                /* Calculating time frame to send RAR PDSCH */
406                ADD_DELTA_TO_TIME(dciTime, rarTime, k0);
407                rarSlot = rarTime.slot;
408
409                for(k2TblIdx = 0; k2TblIdx < msg3K2InfoTbl->k2TimingInfo[rarSlot].numK2; k2TblIdx++)
410                {
411                   k2Index = msg3K2InfoTbl->k2TimingInfo[rarSlot].k2Indexes[k2TblIdx];
412                   k2 = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].k2;
413
414                   /* Delta is added to the slot allocation for msg3 based on 38.214 section 6.1.2.1 */
415                   k2 = k2 + msg3Delta;
416                   if(k2 >= msg3MinSchTime)
417                   {
418                      ADD_DELTA_TO_TIME(rarTime, msg3Time, k2);
419 #ifdef NR_TDD
420                      if(schGetSlotSymbFrmt(msg3Time.slot % totalCfgSlot, cell->slotFrmtBitMap) == DL_SLOT)
421                         continue;
422 #endif
423                      k2Found = true;
424                      break;
425                   }
426                }
427                if(k2Found)
428                   break;
429             }
430          }
431          else if(windowStatus == WINDOW_EXPIRED)
432          {
433             SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
434             SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
435             ueIdx++;
436             continue;
437          }
438
439          /* If K0-K2 combination not found, no scheduling happens */
440          if(!k2Found)
441          {
442             ueIdx++;
443             continue;
444          }
445
446          /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
447          SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
448          if(dciSlotAlloc == NULLP)
449          {
450             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
451             return;
452          }
453          cell->schDlSlotInfo[dciSlot]->rarAlloc = dciSlotAlloc;
454
455          /* Fill PDCCH and PDSCH scheduling information for RAR */
456          if((schFillRar(cell, rarTime, ueIdx, dciSlotAlloc, k0Index)) != ROK)
457          {
458             DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
459             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
460             cell->schDlSlotInfo[dciSlot]->rarAlloc = NULLP;
461             return;
462          }
463
464          /* Allocate resources for msg3 */
465          msg3PuschInfo = schAllocMsg3Pusch(cell->instIdx, cell->raReq[ueIdx]->rachInd->crnti, k2Index, msg3Time);
466          if(msg3PuschInfo)
467          {
468             /* Fill RAR info */
469             dciSlotAlloc->rarInfo.raRnti = cell->raReq[ueIdx]->raRnti;
470             dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueIdx]->rachInd->crnti;
471             dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueIdx]->rachInd->preambleIdx;
472             dciSlotAlloc->rarInfo.ta = cell->raReq[ueIdx]->rachInd->timingAdv;
473             dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb;
474             /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
475             dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
476             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.startPrb;
477             dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.numPrb;
478             dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
479             dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
480             dciSlotAlloc->rarInfo.ulGrant.tpc = 3;  /* TODO : Check appropriate value to be filled */
481             /* Spec 38.213, section 8.2 : In a contention based random access
482              * procedure, the CSI request field is reserved. */
483             dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
484          }
485
486          /* Check if both DCI and RAR are sent in the same slot.
487           * If not, allocate memory RAR PDSCH slot to store RAR info
488           */
489          if(dciSlot == rarSlot)
490             dciSlotAlloc->pduPres = BOTH;
491          else
492          {
493             /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
494             SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
495             if(rarSlotAlloc == NULLP)
496             {
497                DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
498                SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
499                cell->schDlSlotInfo[dciSlot]->rarAlloc = NULLP;
500                return;
501             }
502             cell->schDlSlotInfo[rarSlot]->rarAlloc = rarSlotAlloc;
503
504             /* Copy all RAR info */
505             memcpy(rarSlotAlloc, dciSlotAlloc, sizeof(RarAlloc));
506             rarSlotAlloc->rarPdcchCfg.dci.pdschCfg = &rarSlotAlloc->rarPdschCfg;
507
508             /* Assign correct PDU types in corresponding slots */
509             rarSlotAlloc->pduPres = PDSCH_PDU;
510             dciSlotAlloc->pduPres = PDCCH_PDU;
511             dciSlotAlloc->pdschSlot = rarSlot;  
512          }
513
514          /* Create raCb at SCH */
515          createSchRaCb(cell->raReq[ueIdx]->rachInd->crnti, cell->instIdx);
516
517          SCH_FREE(cell->raReq[ueIdx]->rachInd, sizeof(RachIndInfo));
518          SCH_FREE(cell->raReq[ueIdx], sizeof(SchRaReq));
519       }
520       ueIdx++;
521    } /* End of while(ueIdx < MAX_NUM_UE) */
522 }
523
524 /**
525  * @brief process rach indication function. 
526  *
527  * @details
528  *
529  *     Function : schProcessRachInd
530  *     
531  *     This function process rach indication
532  *     
533  *  @param[in]  rachInd parameters
534  *  @param[in]  shed instance
535  *  @return  ROK
536  **/
537 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
538 {
539    SchCellCb *cell = schCb[schInst].cells[schInst];
540    SchRaReq  *raReq = NULLP;
541    float    slotDuration;
542    uint8_t  winNumSlots;
543    uint8_t  ueIdx;
544
545    if(cell == NULLP)
546    {
547       DU_LOG("\nERROR  -->  SCH: Failed to find cell in schProcessRachInd");
548       return RFAILED;
549    }
550
551    /* Storing RA request in cellCb */
552    GET_UE_IDX(rachInd->crnti, ueIdx);
553    if(ueIdx <= 0)
554    {
555       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
556       return RFAILED;
557    }
558
559    SCH_ALLOC(raReq, sizeof(SchRaReq));
560    if(!raReq)
561    {
562       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in schProcessRachInd");
563       SCH_FREE(rachInd, sizeof(RachIndInfo));
564       return RFAILED;
565    }
566
567    /* calculate the ra-rnti value */
568    raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
569    raReq->rachInd = rachInd;
570    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
571    raReq->winStartTime.slot = rachInd->timingInfo.slot;
572   
573    /* Converting window size from ms to number of slots */
574    slotDuration = (1 / pow(2, cell->cellCfg.numerology));
575    winNumSlots = (float)cell->cellCfg.schRachCfg.raRspWindow / slotDuration;
576    
577    /* Adding window size to window start time to get window end time */
578    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
579
580    cell->raReq[ueIdx -1] = raReq;
581
582    return ROK;
583 }
584
585 /**
586  * @brief fill RAR info function. 
587  *
588  * @details
589  *
590  *     Function : calculateRaRnti
591  *     
592  *     This function fills pdcch and pdsch info for RAR
593  *     
594  *  @param[in]  rar Allocation info
595  *  @param[in]  ra-rnti
596  *  @param[in]  PCI
597  *  @param[in]  offset to pointA to determine freq alloc
598  *  @return  ROK
599  **/
600 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueIdx, RarAlloc *rarAlloc, uint8_t k0Index)
601 {
602    uint8_t  coreset0Idx = 0;
603    uint8_t  firstSymbol = 0, numSymbols = 0;
604    uint8_t  mcs = DEFAULT_MCS;  /* MCS fixed to 4 */
605    uint8_t  dmrsStartSymbol, startSymbol, numSymbol ;
606    uint16_t numRbs = 0;
607    uint16_t tbSize = 0;
608
609    SchBwpDlCfg *initialBwp = &cell->cellCfg.schInitialDlBwp;
610    PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
611    PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
612    BwpCfg *bwp = &rarAlloc->bwp;
613
614    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
615    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
616    numRbs     = coresetIdxTable[coreset0Idx][1]; 
617    numSymbols = coresetIdxTable[coreset0Idx][2];
618
619    /* calculate time domain parameters */
620    // note: since slot value is made sl1, RAR can be sent at all slots
621    uint16_t mask = 0x2000;
622    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
623    {
624       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
625          break;
626       else
627          mask = mask>>1;
628    }
629
630    /* fill BWP */
631    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
632    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
633    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
634    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
635
636    /* fill the PDCCH PDU */
637    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
638    pdcch->coresetCfg.durationSymbols = numSymbols;
639    memcpy(pdcch->coresetCfg.freqDomainResource, \
640       cell->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
641
642    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
643    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
644    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
645    pdcch->coresetCfg.coreSetType = 0;
646    pdcch->coresetCfg.coreSetSize = numRbs;
647    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
648    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
649    pdcch->numDlDci = 1;
650    pdcch->dci.rnti = cell->raReq[ueIdx]->raRnti; /* RA-RNTI */
651    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
652    pdcch->dci.scramblingRnti = 0;
653    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
654    pdcch->dci.aggregLevel = 4;
655    pdcch->dci.beamPdcchInfo.numPrgs = 1;
656    pdcch->dci.beamPdcchInfo.prgSize = 1;
657    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
658    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
659    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
660    pdcch->dci.txPdcchPower.powerValue = 0;
661    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
662    pdcch->dci.pdschCfg = pdsch;
663
664    /* fill the PDSCH PDU */
665    uint8_t cwCount = 0;
666    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
667    pdsch->rnti = cell->raReq[ueIdx]->raRnti; /* RA-RNTI */
668    pdsch->pduIndex = 0;
669    pdsch->numCodewords = 1;
670    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
671    {
672       pdsch->codeword[cwCount].targetCodeRate = 308;
673       pdsch->codeword[cwCount].qamModOrder = 2;
674       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
675       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
676       pdsch->codeword[cwCount].rvIndex = 0;
677       /* RAR PDU length and FAPI payload header length */
678       tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
679       pdsch->codeword[cwCount].tbSize = tbSize;
680    }
681    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
682    pdsch->numLayers = 1;
683    pdsch->transmissionScheme = 0;
684    pdsch->refPoint = 0;
685    pdsch->dmrs.dlDmrsSymbPos = 4;  /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
686    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
687    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
688    pdsch->dmrs.scid = 0;
689    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
690    pdsch->dmrs.dmrsPorts = 0;
691    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
692    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
693    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
694
695    pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
696    pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
697
698    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
699    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
700    pdsch->pdschFreqAlloc.freqAlloc.startPrb = MAX_NUM_RB;
701    pdsch->pdschFreqAlloc.freqAlloc.numPrb = \
702       schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
703
704    /* Find total symbols occupied including DMRS */
705    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
706    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT, 
707     * in that case only PDSCH symbols are marked as occupied */
708    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
709    {
710       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
711       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
712    }
713    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
714    else
715    {
716       startSymbol = dmrsStartSymbol;
717       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
718    }
719
720    /* Allocate the number of PRBs required for RAR PDSCH */
721    if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
722       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
723    {
724       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
725       return RFAILED;
726    }
727
728    pdsch->beamPdschInfo.numPrgs = 1;
729    pdsch->beamPdschInfo.prgSize = 1;
730    pdsch->beamPdschInfo.digBfInterfaces = 0;
731    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
732    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
733    pdsch->txPdschPower.powerControlOffset = 0;
734    pdsch->txPdschPower.powerControlOffsetSS = 0;
735
736    return ROK;
737 }
738
739 /**********************************************************************
740          End of file
741 **********************************************************************/