Round Robin Scheduling of Multi-UE [Issue-ID: ODUHIGH-387]
[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    schCb[schInst].cells[schInst]->raCb[ueIdx -1].msg4recvd = FALSE;
225 }
226
227 /**
228  * @brief resource allocation for msg3 PUSCH
229  *
230  * @details
231  *
232  *     Function : schAllocMsg3Pusch 
233  *     
234  *     This function handles msg3 PUSCH allocation
235  *     
236  *  @param[in]  Inst schInst, SCH instance
237  *  @param[in]  slot, current slot
238  *  @param[out]  msg3StartRb
239  *  @param[out]  msg3NumRb
240  *  @return  void
241  **/
242 SchPuschInfo* schAllocMsg3Pusch(Inst schInst, uint16_t crnti, uint8_t k2Index, SlotTimingInfo msg3SlotTime)
243 {
244    SchCellCb      *cell          = NULLP;
245    SchUlSlotInfo  *schUlSlotInfo = NULLP;
246    uint8_t    mcs       = DEFAULT_MCS;
247    uint8_t    startSymb = 0;
248    uint8_t    symbLen   = 0; 
249    uint16_t   startRb   = 0;
250    uint16_t   numRb     = 0;
251    uint16_t   tbSize    = 0;
252
253    cell = schCb[schInst].cells[schInst];
254    if(cell == NULL)
255    {
256       DU_LOG("\n\nERROR  -->  SCH :  Failed to find cell in schAllocMsg3Pusch");
257       return NULLP;
258    }
259
260    /* Allocate time-domain and frequency-domain resource for MSG3 PUSCH */
261    startSymb = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].startSymbol;
262    symbLen = cell->cellCfg.schInitialUlBwp.puschCommon.timeDomRsrcAllocList[k2Index].symbolLength;
263
264    startRb = MAX_NUM_RB;
265    tbSize = schCalcTbSize(8); /* 6 bytes msg3 and 2 bytes header */
266    numRb = schCalcNumPrb(tbSize, mcs, symbLen);
267    numRb++; /* allocating 1 extra RB for now */
268    allocatePrbUl(cell, msg3SlotTime, startSymb, symbLen, &startRb, numRb);
269
270    /* Fill PUSCH scheduling details in Slot structure */
271    schUlSlotInfo = cell->schUlSlotInfo[msg3SlotTime.slot];
272    SCH_ALLOC(schUlSlotInfo->schPuschInfo, sizeof(SchPuschInfo));
273    if(!schUlSlotInfo->schPuschInfo)
274    {
275       DU_LOG("\nERROR  -->  SCH :  Memory allocation failed in schAllocMsg3Pusch");
276       return NULLP;
277    }
278
279    tbSize = 0;  /* since nPrb has been incremented, recalculating tbSize */
280    tbSize = schCalcTbSizeFromNPrb(numRb, mcs, NUM_PDSCH_SYMBOL);
281    tbSize = tbSize / 8 ; /*bits to byte conversion*/
282
283    schUlSlotInfo->schPuschInfo->crnti             = crnti;
284    schUlSlotInfo->schPuschInfo->harqProcId        = SCH_HARQ_PROC_ID;
285    schUlSlotInfo->schPuschInfo->resAllocType      = SCH_ALLOC_TYPE_1;
286    schUlSlotInfo->schPuschInfo->fdAlloc.startPrb  = startRb;
287    schUlSlotInfo->schPuschInfo->fdAlloc.numPrb    = numRb;
288    schUlSlotInfo->schPuschInfo->tdAlloc.startSymb = startSymb;
289    schUlSlotInfo->schPuschInfo->tdAlloc.numSymb   = symbLen;
290    schUlSlotInfo->schPuschInfo->tbInfo.qamOrder   = QPSK_MODULATION;  /* QPSK modulation */
291    schUlSlotInfo->schPuschInfo->tbInfo.mcs           = mcs;
292    schUlSlotInfo->schPuschInfo->tbInfo.mcsTable   = SCH_MCS_TABLE_QAM_64;
293    schUlSlotInfo->schPuschInfo->tbInfo.ndi        = NEW_TRANSMISSION; /* new transmission */
294    schUlSlotInfo->schPuschInfo->tbInfo.rv               = 0;
295    schUlSlotInfo->schPuschInfo->tbInfo.tbSize     = tbSize;
296    schUlSlotInfo->schPuschInfo->dmrsMappingType   = DMRS_MAP_TYPE_A;  /* Setting Type-A */
297    schUlSlotInfo->schPuschInfo->nrOfDmrsSymbols   = NUM_DMRS_SYMBOLS;
298    schUlSlotInfo->schPuschInfo->dmrsAddPos        = DMRS_ADDITIONAL_POS;
299
300    return schUlSlotInfo->schPuschInfo;
301 }
302
303 /**
304  * @brief Check if a time frame is in RA Response window
305  *
306  * @details
307  *
308  *     Function : isInRaRspWindow
309  *
310  *     Check if a time frame is in RA Response window
311  *
312  *  @param[in]  RA request
313  *  @param[in]  Time frame to check
314  *  @param[in]  Total number of slot per radio frame
315  *  @return  true 
316  *  @return  false
317  **/
318 RaRspWindowStatus isInRaRspWindow(SchRaReq *raReq, SlotTimingInfo frameToCheck, uint16_t numSlotsPerSystemFrame)
319 {
320    uint32_t winStartTime, winEndTime, timeToCheck;
321    
322    winStartTime = (raReq->winStartTime.sfn * numSlotsPerSystemFrame) + raReq->winStartTime.slot;
323    winEndTime = (raReq->winEndTime.sfn * numSlotsPerSystemFrame) + raReq->winEndTime.slot;
324    timeToCheck = (frameToCheck.sfn * numSlotsPerSystemFrame) + frameToCheck.slot;
325
326    /* TODO : check how to handle the wrap around scenario of MAX_SFN */
327    if((timeToCheck >= winStartTime) && (timeToCheck <= winEndTime))
328       return WITHIN_WINDOW;
329    else if(timeToCheck < winStartTime)
330       return WINDOW_YET_TO_START;
331       
332    return WINDOW_EXPIRED;
333 }
334
335 /**
336  * @brief Processes any pending RA request
337  *
338  * @details
339  *
340  *     Function : schProcessRaReq
341  *
342  *     This function process pending RA request
343  *
344  *  @param[in]  Current timing of the cell
345  *  @return  ROK
346  **/
347 bool schProcessRaReq(SchCellCb *cell, SlotTimingInfo currTime, uint8_t ueId)
348 {
349    bool      k2Found = false;
350    uint8_t   k0TblIdx = 0, k2TblIdx = 0;
351    uint8_t   k0Index = 0, k2Index = 0;
352    uint8_t   k0 = 0, k2 = 0;
353    uint8_t   puschMu = 0;
354    uint8_t   msg3Delta = 0, msg3MinSchTime = 0;
355 #ifdef NR_TDD
356    uint8_t   totalCfgSlot = 0;
357 #endif
358    uint16_t             dciSlot = 0, rarSlot = 0;
359    SlotTimingInfo       dciTime, rarTime, msg3Time;
360    RarAlloc             *dciSlotAlloc = NULLP;    /* Stores info for transmission of PDCCH for RAR */
361    RarAlloc             *rarSlotAlloc = NULLP;    /* Stores info for transmission of RAR PDSCH */
362    SchPuschInfo         *msg3PuschInfo = NULLP;   /* Stores MSG3 PUSCH scheduling information */
363    SchK0K1TimingInfoTbl *k0K1InfoTbl=NULLP;    
364    SchK2TimingInfoTbl   *msg3K2InfoTbl=NULLP;
365    RaRspWindowStatus    windowStatus=0;
366
367 #ifdef NR_TDD
368    totalCfgSlot = calculateSlotPatternLength(cell->cellCfg.ssbSchCfg.scsCommon, cell->cellCfg.tddCfg.tddPeriod);
369 #endif
370    k0K1InfoTbl = &cell->cellCfg.schInitialDlBwp.k0K1InfoTbl;
371    msg3K2InfoTbl = &cell->cellCfg.schInitialUlBwp.msg3K2InfoTbl;
372    puschMu = cell->cellCfg.numerology;
373    msg3Delta = puschDeltaTable[puschMu];
374    msg3MinSchTime = minMsg3SchTime[cell->cellCfg.numerology];
375
376    /* Calculating time frame to send DCI for RAR */
377    ADD_DELTA_TO_TIME(currTime, dciTime, PHY_DELTA_DL + SCHED_DELTA);
378    dciSlot = dciTime.slot;
379 #ifdef NR_TDD
380    /* Consider this slot for sending DCI, only if it is a DL slot */
381    if(schGetSlotSymbFrmt(dciSlot, cell->slotFrmtBitMap) == DL_SLOT)
382 #endif
383    {
384       /* If PDCCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
385       if(cell->schDlSlotInfo[dciSlot]->pdcchUe != 0)
386          return false;
387
388       /* Check if this slot is within RA response window */
389       windowStatus = isInRaRspWindow(cell->raReq[ueId-1], dciTime, cell->numSlots);
390       if(windowStatus == WITHIN_WINDOW)
391       {
392          /* For all k0 values, search for a suitable k2 value to schedule MSG3.
393           * RAR DCI, RAR PDSCH and MSG3 is scheduled only if one such k0-k2 combination
394           * is found. Else no scheduling happens. 
395           */
396          for(k0TblIdx = 0; k0TblIdx < k0K1InfoTbl->k0k1TimingInfo[dciSlot].numK0; k0TblIdx++)
397          {
398             k0Index = k0K1InfoTbl->k0k1TimingInfo[dciSlot].k0Indexes[k0TblIdx].k0Index;
399             k0 = cell->cellCfg.schInitialDlBwp.pdschCommon.timeDomRsrcAllocList[k0Index].k0;
400
401             /* Calculating time frame to send RAR PDSCH */
402             ADD_DELTA_TO_TIME(dciTime, rarTime, k0);
403             rarSlot = rarTime.slot;
404             
405             /* If PDSCH is already scheduled on this slot, cannot schedule PDSCH for another UE here. */
406             if(cell->schDlSlotInfo[rarSlot]->pdschUe != 0)
407                continue;
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                   /* If PUSCH is already scheduled on this slot, another PUSCH
424                    * pdu cannot be scheduled here */
425                   if(cell->schUlSlotInfo[msg3Time.slot]->puschUe != 0)
426                      continue;
427
428                   k2Found = true;
429                   break;
430                }
431             }
432             if(k2Found)
433                break;
434          }
435       }
436       else if(windowStatus == WINDOW_EXPIRED)
437       {
438          SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
439          SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
440          return false;
441       }
442
443       /* If K0-K2 combination not found, no scheduling happens */
444       if(!k2Found)
445          return false;
446
447       /* Allocate memory for RAR PDCCH slot, pointer will be checked at schProcessSlotInd() */
448       SCH_ALLOC(dciSlotAlloc, sizeof(RarAlloc));
449       if(dciSlotAlloc == NULLP)
450       {
451          DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for dciSlotAlloc");
452          return false;
453       }
454       cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = dciSlotAlloc;
455
456       /* Fill PDCCH and PDSCH scheduling information for RAR */
457       if((schFillRar(cell, rarTime, ueId, dciSlotAlloc, k0Index)) != ROK)
458       {
459          DU_LOG("\nERROR  -->  SCH: Scheduling of RAR failed in slot [%d]", rarSlot);
460          SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
461          cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
462          return false;
463       }
464
465       /* Allocate resources for msg3 */
466       msg3PuschInfo = schAllocMsg3Pusch(cell->instIdx, cell->raReq[ueId-1]->rachInd->crnti, k2Index, msg3Time);
467       if(msg3PuschInfo)
468       {
469          /* Fill RAR info */
470          dciSlotAlloc->rarInfo.raRnti = cell->raReq[ueId-1]->raRnti;
471          dciSlotAlloc->rarInfo.tcrnti = cell->raReq[ueId-1]->rachInd->crnti;
472          dciSlotAlloc->rarInfo.RAPID = cell->raReq[ueId-1]->rachInd->preambleIdx;
473          dciSlotAlloc->rarInfo.ta = cell->raReq[ueId-1]->rachInd->timingAdv;
474          dciSlotAlloc->rarInfo.ulGrant.bwpSize = cell->cellCfg.schInitialUlBwp.bwp.freqAlloc.numPrb;
475          /* Spec 38.213, section 8.2, 0 : MSG3 PUSCH will be transmitted without frequency hopping */
476          dciSlotAlloc->rarInfo.ulGrant.freqHopFlag = 0;
477          dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.startPrb = msg3PuschInfo->fdAlloc.startPrb;
478          dciSlotAlloc->rarInfo.ulGrant.msg3FreqAlloc.numPrb = msg3PuschInfo->fdAlloc.numPrb;
479          dciSlotAlloc->rarInfo.ulGrant.k2Index = k2Index;
480          dciSlotAlloc->rarInfo.ulGrant.mcs = msg3PuschInfo->tbInfo.mcs;
481          dciSlotAlloc->rarInfo.ulGrant.tpc = 3;  /* TODO : Check appropriate value to be filled */
482          /* Spec 38.213, section 8.2 : In a contention based random access
483           * procedure, the CSI request field is reserved. */
484          dciSlotAlloc->rarInfo.ulGrant.csiReq = 0;
485       }
486
487       /* Check if both DCI and RAR are sent in the same slot.
488        * If not, allocate memory RAR PDSCH slot to store RAR info
489        */
490       if(dciSlot == rarSlot)
491          dciSlotAlloc->pduPres = BOTH;
492       else
493       {
494          /* Allocate memory to schedule rarSlot to send RAR, pointer will be checked at schProcessSlotInd() */
495          SCH_ALLOC(rarSlotAlloc, sizeof(RarAlloc));
496          if(rarSlotAlloc == NULLP)
497          {
498             DU_LOG("\nERROR  -->  SCH : Memory Allocation failed for rarSlotAlloc");
499             SCH_FREE(dciSlotAlloc, sizeof(RarAlloc));
500             cell->schDlSlotInfo[dciSlot]->rarAlloc[ueId-1] = NULLP;
501             return false;
502          }
503          cell->schDlSlotInfo[rarSlot]->rarAlloc[ueId-1] = rarSlotAlloc;
504
505          /* Copy all RAR info */
506          memcpy(rarSlotAlloc, dciSlotAlloc, sizeof(RarAlloc));
507          rarSlotAlloc->rarPdcchCfg.dci.pdschCfg = &rarSlotAlloc->rarPdschCfg;
508
509          /* Assign correct PDU types in corresponding slots */
510          rarSlotAlloc->pduPres = PDSCH_PDU;
511          dciSlotAlloc->pduPres = PDCCH_PDU;
512          dciSlotAlloc->pdschSlot = rarSlot;  
513       }
514
515       cell->schDlSlotInfo[dciSlot]->pdcchUe = ueId;
516       cell->schDlSlotInfo[rarSlot]->pdschUe = ueId;
517       cell->schUlSlotInfo[msg3Time.slot]->puschUe = ueId;
518
519       /* Create raCb at SCH */
520       createSchRaCb(cell->raReq[ueId-1]->rachInd->crnti, cell->instIdx);
521
522       /* Remove RachInd from pending RA request list */
523       SCH_FREE(cell->raReq[ueId-1]->rachInd, sizeof(RachIndInfo));
524       SCH_FREE(cell->raReq[ueId-1], sizeof(SchRaReq));
525       
526       return true;
527    }
528    return false;
529 }
530
531 /**
532  * @brief process rach indication function. 
533  *
534  * @details
535  *
536  *     Function : schProcessRachInd
537  *     
538  *     This function process rach indication
539  *     
540  *  @param[in]  rachInd parameters
541  *  @param[in]  shed instance
542  *  @return  ROK
543  **/
544 uint8_t schProcessRachInd(RachIndInfo *rachInd, Inst schInst)
545 {
546    SchCellCb *cell = schCb[schInst].cells[schInst];
547    SchRaReq  *raReq = NULLP;
548    float    slotDuration;
549    uint8_t  winNumSlots;
550    uint8_t  ueIdx;
551
552    if(cell == NULLP)
553    {
554       DU_LOG("\nERROR  -->  SCH: Failed to find cell in schProcessRachInd");
555       return RFAILED;
556    }
557
558    /* Storing RA request in cellCb */
559    GET_UE_IDX(rachInd->crnti, ueIdx);
560    if(ueIdx <= 0)
561    {
562       DU_LOG("\nERROR  -->  SCH: Invalid CRNTI [%d]", rachInd->crnti);
563       return RFAILED;
564    }
565
566    SCH_ALLOC(raReq, sizeof(SchRaReq));
567    if(!raReq)
568    {
569       DU_LOG("\nERROR  -->  SCH : Memory allocation failure in schProcessRachInd");
570       SCH_FREE(rachInd, sizeof(RachIndInfo));
571       return RFAILED;
572    }
573
574    /* calculate the ra-rnti value */
575    raReq->raRnti = calculateRaRnti(rachInd->symbolIdx, rachInd->slotIdx, rachInd->freqIdx);
576    raReq->rachInd = rachInd;
577    raReq->winStartTime.sfn = rachInd->timingInfo.sfn;
578    raReq->winStartTime.slot = rachInd->timingInfo.slot;
579   
580    /* Converting window size from ms to number of slots */
581    slotDuration = (1 / pow(2, cell->cellCfg.numerology));
582    winNumSlots = (float)cell->cellCfg.schRachCfg.raRspWindow / slotDuration;
583    
584    /* Adding window size to window start time to get window end time */
585    ADD_DELTA_TO_TIME(raReq->winStartTime, raReq->winEndTime, winNumSlots);
586    cell->raReq[ueIdx -1] = raReq;
587
588    /* Adding UE Id to list of pending UEs to be scheduled */
589    addUeToBeScheduled(cell, ueIdx);
590
591    return ROK;
592 }
593
594 /**
595  * @brief fill RAR info function. 
596  *
597  * @details
598  *
599  *     Function : calculateRaRnti
600  *     
601  *     This function fills pdcch and pdsch info for RAR
602  *     
603  *  @param[in]  rar Allocation info
604  *  @param[in]  ra-rnti
605  *  @param[in]  PCI
606  *  @param[in]  offset to pointA to determine freq alloc
607  *  @return  ROK
608  **/
609 uint8_t schFillRar(SchCellCb *cell, SlotTimingInfo rarTime, uint16_t ueId, RarAlloc *rarAlloc, uint8_t k0Index)
610 {
611    uint8_t  coreset0Idx = 0;
612    uint8_t  firstSymbol = 0, numSymbols = 0;
613    uint8_t  mcs = DEFAULT_MCS;  /* MCS fixed to 4 */
614    uint8_t  dmrsStartSymbol, startSymbol, numSymbol ;
615    uint16_t numRbs = 0;
616    uint16_t tbSize = 0;
617
618    SchBwpDlCfg *initialBwp = &cell->cellCfg.schInitialDlBwp;
619    PdcchCfg *pdcch = &rarAlloc->rarPdcchCfg;
620    PdschCfg *pdsch = &rarAlloc->rarPdschCfg;
621    BwpCfg *bwp = &rarAlloc->bwp;
622
623    /* derive the sib1 coreset0 params from table 13-1 spec 38.213 */
624    coreset0Idx     = initialBwp->pdcchCommon.commonSearchSpace.coresetId;
625    numRbs     = coresetIdxTable[coreset0Idx][1]; 
626    numSymbols = coresetIdxTable[coreset0Idx][2];
627
628    /* calculate time domain parameters */
629    // note: since slot value is made sl1, RAR can be sent at all slots
630    uint16_t mask = 0x2000;
631    for(firstSymbol=0; firstSymbol<MAX_SYMB_PER_SLOT; firstSymbol++)
632    {
633       if(initialBwp->pdcchCommon.commonSearchSpace.monitoringSymbol & mask)
634          break;
635       else
636          mask = mask>>1;
637    }
638
639    /* fill BWP */
640    bwp->freqAlloc.numPrb   = initialBwp->bwp.freqAlloc.numPrb;
641    bwp->freqAlloc.startPrb = initialBwp->bwp.freqAlloc.startPrb;
642    bwp->subcarrierSpacing  = initialBwp->bwp.scs;
643    bwp->cyclicPrefix       = initialBwp->bwp.cyclicPrefix;
644
645    /* fill the PDCCH PDU */
646    pdcch->coresetCfg.startSymbolIndex = firstSymbol;
647    pdcch->coresetCfg.durationSymbols = numSymbols;
648    memcpy(pdcch->coresetCfg.freqDomainResource, \
649       cell->cellCfg.schInitialDlBwp.pdcchCommon.commonSearchSpace.freqDomainRsrc, FREQ_DOM_RSRC_SIZE);
650
651    pdcch->coresetCfg.cceRegMappingType = 1; /* coreset0 is always interleaved */
652    pdcch->coresetCfg.regBundleSize = 6;    /* spec-38.211 sec 7.3.2.2 */
653    pdcch->coresetCfg.interleaverSize = 2;  /* spec-38.211 sec 7.3.2.2 */
654    pdcch->coresetCfg.coreSetType = 0;
655    pdcch->coresetCfg.coreSetSize = numRbs;
656    pdcch->coresetCfg.shiftIndex = cell->cellCfg.phyCellId;
657    pdcch->coresetCfg.precoderGranularity = 0; /* sameAsRegBundle */
658    pdcch->numDlDci = 1;
659    pdcch->dci.rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
660    pdcch->dci.scramblingId = cell->cellCfg.phyCellId;
661    pdcch->dci.scramblingRnti = 0;
662    pdcch->dci.cceIndex = 4; /* considering SIB1 is sent at cce 0-1-2-3 */
663    pdcch->dci.aggregLevel = 4;
664    pdcch->dci.beamPdcchInfo.numPrgs = 1;
665    pdcch->dci.beamPdcchInfo.prgSize = 1;
666    pdcch->dci.beamPdcchInfo.digBfInterfaces = 0;
667    pdcch->dci.beamPdcchInfo.prg[0].pmIdx = 0;
668    pdcch->dci.beamPdcchInfo.prg[0].beamIdx[0] = 0;
669    pdcch->dci.txPdcchPower.powerValue = 0;
670    pdcch->dci.txPdcchPower.powerControlOffsetSS = 0;
671    pdcch->dci.pdschCfg = pdsch;
672
673    /* fill the PDSCH PDU */
674    uint8_t cwCount = 0;
675    pdsch->pduBitmap = 0; /* PTRS and CBG params are excluded */
676    pdsch->rnti = cell->raReq[ueId-1]->raRnti; /* RA-RNTI */
677    pdsch->pduIndex = 0;
678    pdsch->numCodewords = 1;
679    for(cwCount = 0; cwCount < pdsch->numCodewords; cwCount++)
680    {
681       pdsch->codeword[cwCount].targetCodeRate = 308;
682       pdsch->codeword[cwCount].qamModOrder = 2;
683       pdsch->codeword[cwCount].mcsIndex = mcs; /* mcs configured to 4 */
684       pdsch->codeword[cwCount].mcsTable = 0;   /* notqam256 */
685       pdsch->codeword[cwCount].rvIndex = 0;
686       /* RAR PDU length and FAPI payload header length */
687       tbSize = schCalcTbSize(RAR_PAYLOAD_SIZE + TX_PAYLOAD_HDR_LEN);
688       pdsch->codeword[cwCount].tbSize = tbSize;
689    }
690    pdsch->dataScramblingId = cell->cellCfg.phyCellId;
691    pdsch->numLayers = 1;
692    pdsch->transmissionScheme = 0;
693    pdsch->refPoint = 0;
694    pdsch->dmrs.dlDmrsSymbPos = 4;  /* Bitmap value 00000000000100 i.e. using 3rd symbol for PDSCH DMRS */
695    pdsch->dmrs.dmrsConfigType = 0; /* type-1 */
696    pdsch->dmrs.dlDmrsScramblingId = cell->cellCfg.phyCellId;
697    pdsch->dmrs.scid = 0;
698    pdsch->dmrs.numDmrsCdmGrpsNoData = 1;
699    pdsch->dmrs.dmrsPorts = 0;
700    pdsch->dmrs.mappingType      = DMRS_MAP_TYPE_A;  /* Type-A */
701    pdsch->dmrs.nrOfDmrsSymbols  = NUM_DMRS_SYMBOLS;
702    pdsch->dmrs.dmrsAddPos       = DMRS_ADDITIONAL_POS;
703
704    pdsch->pdschTimeAlloc.rowIndex = k0Index;
705    pdsch->pdschTimeAlloc.timeAlloc.startSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].startSymbol;
706    pdsch->pdschTimeAlloc.timeAlloc.numSymb = initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol;
707
708    pdsch->pdschFreqAlloc.vrbPrbMapping = 0; /* non-interleaved */
709    pdsch->pdschFreqAlloc.resourceAllocType = 1; /* RAT type-1 RIV format */
710    pdsch->pdschFreqAlloc.freqAlloc.startPrb = MAX_NUM_RB;
711    pdsch->pdschFreqAlloc.freqAlloc.numPrb = \
712       schCalcNumPrb(tbSize, mcs, initialBwp->pdschCommon.timeDomRsrcAllocList[k0Index].lengthSymbol);
713
714    /* Find total symbols occupied including DMRS */
715    dmrsStartSymbol = findDmrsStartSymbol(pdsch->dmrs.dlDmrsSymbPos);
716    /* If there are no DRMS symbols, findDmrsStartSymbol() returns MAX_SYMB_PER_SLOT, 
717     * in that case only PDSCH symbols are marked as occupied */
718    if(dmrsStartSymbol == MAX_SYMB_PER_SLOT)
719    {
720       startSymbol = pdsch->pdschTimeAlloc.timeAlloc.startSymb;
721       numSymbol = pdsch->pdschTimeAlloc.timeAlloc.numSymb;
722    }
723    /* If DMRS symbol is found, mark DMRS and PDSCH symbols as occupied */
724    else
725    {
726       startSymbol = dmrsStartSymbol;
727       numSymbol = pdsch->dmrs.nrOfDmrsSymbols + pdsch->pdschTimeAlloc.timeAlloc.numSymb;
728    }
729
730    /* Allocate the number of PRBs required for RAR PDSCH */
731    if((allocatePrbDl(cell, rarTime, startSymbol, numSymbol,\
732       &pdsch->pdschFreqAlloc.freqAlloc.startPrb, pdsch->pdschFreqAlloc.freqAlloc.numPrb)) != ROK)
733    {
734       DU_LOG("\nERROR  --> SCH : allocatePrbDl() failed for RAR");
735       return RFAILED;
736    }
737
738    pdsch->beamPdschInfo.numPrgs = 1;
739    pdsch->beamPdschInfo.prgSize = 1;
740    pdsch->beamPdschInfo.digBfInterfaces = 0;
741    pdsch->beamPdschInfo.prg[0].pmIdx = 0;
742    pdsch->beamPdschInfo.prg[0].beamIdx[0] = 0;
743    pdsch->txPdschPower.powerControlOffset = 0;
744    pdsch->txPdschPower.powerControlOffsetSS = 0;
745
746    return ROK;
747 }
748
749 /**********************************************************************
750          End of file
751 **********************************************************************/